sqlite - creating and upgrading data base in android -
i playing sqlite , android. far app has 2 activities..
a main activity:
import android.app.activity; import android.content.sharedpreferences; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.util.log; public class homefavesactivity extends activity { private static final string tag = "homefavescatovoty"; sqlitedatabase lcdb; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); log.v(tag, "created"); databasemanager db = new databasemanager(this); db.getwritabledatabase(); } }
and databasemanager class:
import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; import android.widget.toast; public class databasemanager extends sqliteopenhelper{ private static final string tag = "databasemanager"; static final string dbname ="lcinstore"; static final string allicons = "icons"; static final string homeicons = "homescreenicons"; static final string coliconid = "iconid"; static final string colid = "id"; static final string colimage = "iconimage"; static final string collabel = "iconlabel"; static final string colname = "iconname"; public databasemanager(context context) { super(context, dbname, null, 4); // starting value 1 must change on upgrades log.v(tag, "initaited"); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub log.v(tag, "on create called"); // create icon table if not exist db.execsql("create table "+ allicons +"" + "("+coliconid + " integer primary key autoincrement, "+ colname + " text," + colimage + " text," + collabel + "text)"); // create homescreen icons table if not exist db.execsql("create table if not exist " + homeicons+"" + "("+colid+" integer not null, foreign key ("+colid+") references " + ""+allicons+" ("+coliconid+"));"); inserticons(db); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub log.v(tag, "on upgrade called"); db.execsql("drop table if exists "+allicons); inserticons(db); } private void inserticons(sqlitedatabase db) { contentvalues cv=new contentvalues(); cv.put(coliconid, 1); cv.put(colname, "icon1"); cv.put(colimage, "icon_one"); cv.put(collabel, "one"); db.insert(allicons, coliconid, cv); cv.put(coliconid, 2); cv.put(colname, "icon2"); cv.put(colimage, "icon_two"); cv.put(collabel, "two"); db.insert(allicons, coliconid, cv); cv.put(coliconid, 3); cv.put(colname, "icon3"); cv.put(colimage, "icon_three"); cv.put(collabel, "three"); db.insert(allicons, coliconid, cv); db.close(); } public void addhomescreenicon(int id){ sqlitedatabase db=this.getwritabledatabase(); cursor c = db.rawquery("select * "+ homeicons +" colid = " +id, null); int numfound = c.getcount(); if(numfound<1){ contentvalues cv=new contentvalues(); cv.put(colid, id); db.insert(homeicons, colid, cv); } // else nothing } public void deletehomescreenicon (int id){ sqlitedatabase db=this.getwritabledatabase(); db.delete(homeicons, "id = " +id, null); // //need find row of homeicons table matches id , delete db.close(); } }
you noticed have 4 logs being created.. 1 in main activity' on create, 1 in databasemanager's constructor, oncreate, , onupgrade using these logs try , see when database created , or upgraded ect.. far logs fire homefavesactivity , 1 in constructor of databasemanager. other 2 not firing. how create database, , how upgrade should want to?
exception error:
03-07 12:06:33.459: d/androidruntime(1257): shutting down vm 03-07 12:06:33.459: w/dalvikvm(1257): threadid=1: thread exiting uncaught exception (group=0x401df760) 03-07 12:06:33.459: e/androidruntime(1257): fatal exception: main 03-07 12:06:33.459: e/androidruntime(1257): java.lang.runtimeexception: unable start activity componentinfo{com.sapient/com.sapient.homefavesactivity}: android.database.sqlite.sqliteexception: near "exist": syntax error 03-07 12:06:33.459: e/androidruntime(1257): @ android.app.activitythread.performlaunchactivity(activitythread.java:1751) 03-07 12:06:33.459: e/androidruntime(1257): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1767) 03-07 12:06:33.459: e/androidruntime(1257): @ android.app.activitythread.access$1500(activitythread.java:122) 03-07 12:06:33.459: e/androidruntime(1257): @ android.app.activitythread$h.handlemessage(activitythread.java:1005) 03-07 12:06:33.459: e/androidruntime(1257): @ android.os.handler.dispatchmessage(handler.java:99) 03-07 12:06:33.459: e/androidruntime(1257): @ android.os.looper.loop(looper.java:132) 03-07 12:06:33.459: e/androidruntime(1257): @ android.app.activitythread.main(activitythread.java:4028) 03-07 12:06:33.459: e/androidruntime(1257): @ java.lang.reflect.method.invokenative(native method) 03-07 12:06:33.459: e/androidruntime(1257): @ java.lang.reflect.method.invoke(method.java:491) 03-07 12:06:33.459: e/androidruntime(1257): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:844) 03-07 12:06:33.459: e/androidruntime(1257): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:602) 03-07 12:06:33.459: e/androidruntime(1257): @ dalvik.system.nativestart.main(native method) 03-07 12:06:33.459: e/androidruntime(1257): caused by: android.database.sqlite.sqliteexception: near "exist": syntax error 03-07 12:06:33.459: e/androidruntime(1257): @ android.database.sqlite.sqlitestatement.native_executesql(native method) 03-07 12:06:33.459: e/androidruntime(1257): @ android.database.sqlite.sqlitestatement.executeupdatedelete(sqlitestatement.java:89) 03-07 12:06:33.459: e/androidruntime(1257): @ android.database.sqlite.sqlitedatabase.executesql(sqlitedatabase.java:1933) 03-07 12:06:33.459: e/androidruntime(1257): @ android.database.sqlite.sqlitedatabase.execsql(sqlitedatabase.java:1864) 03-07 12:06:33.459: e/androidruntime(1257): @ com.sapient.databasemanager.oncreate(databasemanager.java:45) 03-07 12:06:33.459: e/androidruntime(1257): @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:157) 03-07 12:06:33.459: e/androidruntime(1257): @ com.sapient.homefavesactivity.oncreate(homefavesactivity.java:22) 03-07 12:06:33.459: e/androidruntime(1257): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1048) 03-07 12:06:33.459: e/androidruntime(1257): @ android.app.activitythread.performlaunchactivity(activitythread.java:1715) 03-07 12:06:33.459: e/androidruntime(1257): ... 11 more
you enter in methods when call either getwritabledatabase()
or getreadabledatabse()
on databasemanager
instance db.
db.getwritabledatabase();
or
db.getreadabledatabase();
Comments
Post a Comment