Eclipse LogCat Error : android.database.sqlite.SQLiteException: no such table: -
public class dbhelper extends sqliteopenhelper{ //the android's default system path of application database. private static string db_path = "/data/data/com.tmm.android.chuck/databases/"; private static string db_name = "questionsdb"; private sqlitedatabase mydatabase; private final context mycontext; /** * constructor * takes , keeps reference of passed context in order access application assets , resources. * @param context */ public dbhelper(context context) { super(context, db_name, null, 1); this.mycontext = context; } /** * creates empty database on system , rewrites own database. * */ public void createdatabase() throws ioexception{ boolean dbexist = checkdatabase(); if(!dbexist) { //by calling method , empty database created default system path //of application gonna able overwrite database our database. this.getreadabledatabase(); try { copydatabase(); } catch (ioexception e) { throw new error("error copying database"); } } } /** * check if database exist avoid re-copying file each time open application. * @return true if exists, false if doesn't */ private boolean checkdatabase(){ sqlitedatabase checkdb = null; try{ string mypath = db_path + db_name; checkdb = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); }catch(sqliteexception e){ //database does't exist yet. } if(checkdb != null){ checkdb.close(); } return checkdb != null ? true : false; } /** * copies database local assets-folder created empty database in * system folder, can accessed , handled. * done transfering bytestream. * */ private void copydatabase() throws ioexception{ //open local db input stream inputstream myinput = mycontext.getassets().open(db_name); // path created empty db string outfilename = db_path + db_name; //open empty db output stream outputstream myoutput = new fileoutputstream(outfilename); //transfer bytes inputfile outputfile byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer))>0){ myoutput.write(buffer, 0, length); } //close streams myoutput.flush(); myoutput.close(); myinput.close(); } public void opendatabase() throws sqlexception{ //open database string mypath = db_path + db_name; mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); } @override public synchronized void close() { if(mydatabase != null) mydatabase.close(); super.close(); } @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } // add public helper methods access , content database. // return cursors doing "return mydatabase.query(....)" it'd easy // create adapters views. public list<question> getquestionset(int difficulty, int numq){ list<question> questionset = new arraylist<question>(); cursor c = mydatabase.rawquery("select * questions difficulty=" + difficulty + " order random() limit " + numq, null); while (c.movetonext()){ //log.d("question", "question found in db: " + c.getstring(1)); question q = new question(); q.setquestion(c.getstring(1)); q.setanswer(c.getstring(2)); q.setoption1(c.getstring(3)); q.setoption2(c.getstring(4)); q.setoption3(c.getstring(5)); q.setrating(difficulty); questionset.add(q); } return questionset; } }
logcat error message:
android.database.sqlite.sqliteexception: no such table: question (code 1): , while compiling: select * question difficulty=2 order random() limit 20
i have added database in assets folder. has table "question
" coloumns _id
, "question
", "answer
", "option1
", "option2
" , "option3
", "difficulty
".
try class helpful you.
you need sure old database not in device currently, can reinstall application.
public class databasehepler extends sqliteopenhelper{ public databasehepler(context context) { super(context, database_name, null, database_version); this.mycontext = context; try { createdatabase(); //bugsensehandler.setup(this.mycontext, "0670ce92"); //bugsensehandler.readlogs(); } catch (exception e) { log.e(tag,"databasehelper_constuctor createdatabase :" + e.fillinstacktrace()); } } private static final string tag = "databasehelper"; //your database name private static final string database_name = "questiondb.sqlite"; //your database version private static final int database_version = 1; private final context mycontext; private static sqlitedatabase mydatabase; //the android's default system path of application database. db_path = "/data/data/your_package/databases/" private static string db_path = "/data/data/com.tmm.android.chuck/databases/"; //***************************** constructor ***************************** /** * constructor * takes , keeps reference of passed context in order access application assets , resources. * @param context **/ //***************************** sqliteopenhelper's methods ***************************** @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } @override public synchronized void close() { if(mydatabase != null) mydatabase.close(); super.close(); } //***************************** db handler methods ***************************** /** * creates empty database on system , rewrites own database. **/ public void createdatabase() throws ioexception { boolean dbexist = checkdatabase(); if(dbexist) { //database exist opendatabase(); } else { //by calling method , empty database created default system path //of application gonna able overwrite database our database. mydatabase = getwritabledatabase(); try { copydatabase(); } catch (ioexception e) { throw new error("error createdatabase()."); } } } /** * check if database exist avoid re-copying file each time open application. * @return true if exists, false if doesn't **/ private boolean checkdatabase() { sqlitedatabase checkdb = null; try { string mypath = db_path + database_name; checkdb = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite); } catch(sqliteexception e) { //database does't exist yet. } if(checkdb != null) { checkdb.close(); } return checkdb != null ? true : false; } /*public boolean isdatabaseexist() { file dbfile = new file(db_path + database_name); return dbfile.exists(); }*/ /** * copies database local assets-folder created empty database in * system folder, can accessed , handled. * done transferring bytestream. **/ private void copydatabase() throws ioexception { try { //open local db input stream inputstream myinput = mycontext.getassets().open("database_name"); // path created empty db string outfilename = db_path + database_name; //open empty db output stream outputstream myoutput = new fileoutputstream(outfilename); //transfer bytes inputfile outputfile byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer))>0) { myoutput.write(buffer, 0, length); } //close streams myoutput.flush(); myoutput.close(); myinput.close(); } catch (ioexception e) { throw new error("error copydatabase()."); } } public void opendatabase() throws sqlexception, ioexception { try { //open database string mypath = db_path + database_name; mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite); } catch (sqliteexception e) { throw new error("error opendatabase()."); } } public static list<itemclass> getanswerlist() { list<itemclass> resultlist = new arraylist<reminderitemclass>(); itemclass obj; cursor cursor; string query; query = "select * tblname"; try { cursor = mydatabase.rawquery(query,null); if(cursor != null) { while(cursor.movetonext()) { obj=new reminderitemclass(); obj.id=cursor.getstring(0); obj.title=cursor.getstring(1); . . resultlist.add(obj); } } cursor.deactivate(); } catch (exception e) { log.e(tag,"error : " + e.fillinstacktrace()); } return resultlist; } }
you not copying database coding, if have other problem let me know.
hopefully solve problem.
Comments
Post a Comment