Sqlite database practice with android not working -
i have made simple demo application of sqlite databse in android .inthat activity contains edittext , button , textxview, have created handler class , implemented java code mainactivity,but not working..i have made below link : http://stdioe.blogspot.in/2012/03/how-to-connect-sqlite-database-in.html
but shows error in myactivity.java file in line "private db names;"....please me...what is?
my code below:
**main.xml** <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <edittext android:id="@+id/edittext1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="68dp" android:layout_margintop="48dp" android:ems="10" android:inputtype="textpersonname" > <requestfocus /> </edittext> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edittext1" android:layout_centerhorizontal="true" android:layout_margintop="43dp" android:text="button" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/button1" android:layout_centervertical="true" android:text="" android:textappearance="?android:attr/textappearancelarge" /> </relativelayout>
dbcontroller.java
package com.example.dbbb; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper; public class dbcontroller extends sqliteopenhelper { public static final string my_database ="name"; public static final int version =1; public dbcontroller(context context) { super(context, my_database, null, version); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub db.execsql("create table mynames(id integer primary key autoincrement,name text);"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql("drop table if exists mynames"); oncreate(db); } }
main.java
package com.example.dbbb; import android.os.bundle; import android.app.activity; import android.app.listactivity; import android.content.contentvalues; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.listview; import android.widget.textview; public class mainactivity extends listactivity { edittext et; button b; private db names; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); et=(edittext)findviewbyid(r.id.edittext1); b=(button)findviewbyid(r.id.button1); names = new db(this); b.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub try{ adddata(name.gettext().tostring()); cursor cursor = showdata(); showdata(cursor); } finally{ names.close(); } } }); } private void adddata(string resultname){ sqlitedatabase db = names.getwritabledatabase(); contentvalues datas = new contentvalues(); datas.put("name", resultname); db.insertorthrow("ournames", null, datas); } private string[] select = {"id", "name"}; private cursor showdata(){ sqlitedatabase db = names.getreadabledatabase(); cursor cursor = db.query("ournames", select, null, null, null, null, null); startmanagingcursor(cursor); return cursor; } private void showdata(cursor cursor){ stringbuilder builder = new stringbuilder("results!:\n"); while(cursor.movetonext()){ string whatthenameis = cursor.getstring((cursor.getcolumnindex("name"))); builder.append(whatthenameis).append("\n"); } textview text = (textview)findviewbyid(r.id.textview1); text.settext(builder); } }
but shows error in myactivity.java file in line "private db names;"....please me...what is?
you referring line:
private db names;
you have not imported class db
in mainactivity
, , trying use it. eclipse cannot resolve class object, shows error on line.
you can try source -> organize imports
import right classes.
edit
looking @ blog, looks referring implementation of sqliteopenhelper
when use db
, apparently whoever wrote never tried compile it. change db
dbcontroller
code:
here:
private dbcontroller names;
and here:
names = new dbcontroller(this);
Comments
Post a Comment