android - How to deal with "No Such Table Exception" -
in android activity, trying retrieve data existing database. copied db in assets , tried access showing error no such table exists while compiling table columns. tried tutorials none of them worked. should do?
below code causing problem.
database adapter class:
public static final string database_name="wetrip"; public static final string database_table="tour"; public static final int database_version=1; public final string tag = "dbadapter"; public static final string key_id = "_id"; public static final string key_tour = "tour_name"; public static final string key_days = "days"; public static final string key_nights = "nights"; final context ctx; databasehelper dbhelper; sqlitedatabase sqlitedb; public toursopenhelper(context context) { this.ctx = context; dbhelper = new databasehelper(context); } private static class databasehelper extends sqliteopenhelper{ databasehelper(context ctxt) { // todo auto-generated constructor stub super(ctxt, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub } } //--open db public toursopenhelper open() throws sqlexception{ sqlitedb = dbhelper.getwritabledatabase(); return this; } public void close() { dbhelper.close(); } public cursor getalldata() { return sqlitedb.query(database_table, new string[] {key_id,key_tour,key_days,key_nights}, null,null,null,null,null); }
databaseactivity:
toursopenhelper toh = new toursopenhelper(this); try{ string destpath = "/data/data/"+getpackagename()+"/databases"; file f = new file(destpath); if(!f.exists()){ f.mkdirs(); f.createnewfile(); //--copy db assets copydb(getbasecontext().getassets().open("wetrip"), new fileoutputstream(destpath+"/wetrip")); } } catch(filenotfoundexception e){ e.printstacktrace(); } catch(ioexception e){ e.printstacktrace(); } //--get data toh.open(); cursor c = toh.getalldata(); if(c.movetofirst()){ do{ displaycontact(c); }while(c.movetonext()); } toh.close(); } public void copydb(inputstream in, outputstream out) throws ioexception { byte[] buffer = new byte[1024]; int length; while((length = in.read(buffer)) >0){ out.write(buffer,0,length); } in.close(); out.close(); } public void displaycontact(cursor cursor) { toast.maketext(this, "id: "+cursor.getstring(0)+"\n"+"name: "+cursor.getstring(2)+"\n"+"days: "+cursor.getstring(4)+"\n"+"nights: "+cursor.getstring(3)+"\n", toast.length_long).show(); }
logcat output:
08-10 14:19:41.423: e/androidruntime(12764): java.lang.runtimeexception: unable start activity componentinfo{com.example.travelplanner/com.example.travelplanner.databaseactivity}: android.database.sqlite.sqliteexception: no such table: tour: , while compiling: select _id, tour_name, days, nights tour 08-10 14:19:41.423: e/androidruntime(12764): @ android.app.activitythread.performlaunchactivity(activitythread.java:1967) 08-10 14:19:41.423: e/androidruntime(12764): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1992) 08-10 14:19:41.423: e/androidruntime(12764): @ android.app.activitythread.access$600(activitythread.java:127) 08-10 14:19:41.423: e/androidruntime(12764): @ android.app.activitythread$h.handlemessage(activitythread.java:1158) 08-10 14:19:41.423: e/androidruntime(12764): @ android.os.handler.dispatchmessage(handler.java:99) 08-10 14:19:41.423: e/androidruntime(12764): @ android.os.looper.loop(looper.java:137) 08-10 14:19:41.423: e/androidruntime(12764): @ android.app.activitythread.main(activitythread.java:4448) 08-10 14:19:41.423: e/androidruntime(12764): @ java.lang.reflect.method.invokenative(native method) 08-10 14:19:41.423: e/androidruntime(12764): @ java.lang.reflect.method.invoke(method.java:511) 08-10 14:19:41.423: e/androidruntime(12764): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:823) 08-10 14:19:41.423: e/androidruntime(12764): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:590) 08-10 14:19:41.423: e/androidruntime(12764): @ dalvik.system.nativestart.main(native method) 08-10 14:19:41.423: e/androidruntime(12764): caused by: android.database.sqlite.sqliteexception: no such table: tour: , while compiling: select _id, tour_name, days, nights tour 08-10 14:19:41.423: e/androidruntime(12764): @ android.database.sqlite.sqlitecompiledsql.native_compile(native method) 08-10 14:19:41.423: e/androidruntime(12764): @ android.database.sqlite.sqlitecompiledsql.<init>(sqlitecompiledsql.java:68) 08-10 14:19:41.423: e/androidruntime(12764): @ android.database.sqlite.sqliteprogram.compilesql(sqliteprogram.java:143) 08-10 14:19:41.423: e/androidruntime(12764): @ android.database.sqlite.sqliteprogram.compileandbindallargs(sqliteprogram.java:361) 08-10 14:19:41.423: e/androidruntime(12764): @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:127) 08-10 14:19:41.423: e/androidruntime(12764): @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:94) 08-10 14:19:41.423: e/androidruntime(12764): @ android.database.sqlite.sqlitequery.<init>(sqlitequery.java:53) 08-10 14:19:41.423: e/androidruntime(12764): @ android.database.sqlite.sqlitedirectcursordriver.query(sqlitedirectcursordriver.java:47) 08-10 14:19:41.423: e/androidruntime(12764): @ android.database.sqlite.sqlitedatabase.rawquerywithfactory(sqlitedatabase.java:1570) 08-10 14:19:41.423: e/androidruntime(12764): @ android.database.sqlite.sqlitedatabase.querywithfactory(sqlitedatabase.java:1453) 08-10 14:19:41.423: e/androidruntime(12764): @ android.database.sqlite.sqlitedatabase.query(sqlitedatabase.java:1409) 08-10 14:19:41.423: e/androidruntime(12764): @ android.database.sqlite.sqlitedatabase.query(sqlitedatabase.java:1489) 08-10 14:19:41.423: e/androidruntime(12764): @ com.example.travelplanner.toursopenhelper.getalldata(toursopenhelper.java:59) 08-10 14:19:41.423: e/androidruntime(12764): @ com.example.travelplanner.databaseactivity.oncreate(databaseactivity.java:46) 08-10 14:19:41.423: e/androidruntime(12764): @ android.app.activity.performcreate(activity.java:4465) 08-10 14:19:41.423: e/androidruntime(12764): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1049) 08-10 14:19:41.423: e/androidruntime(12764): @ android.app.activitythread.performlaunchactivity(activitythread.java:1931) 08-10 14:19:41.423: e/androidruntime(12764): ... 11 more
http://www.youtube.com/watch?v=a2nui31idls
may helpful part 111 till 124 awesome tutorials recommend watch it
Comments
Post a Comment