android: SQLite Exception: no suche table in existing database with three tables -
i have problem many have , think, tried solutions, have not found right solution yet.
my existing database "base.sqlite3" in "assets" folder, contains 3 tables. when want query, appears error, table not there.
(in code possible syntax errors, cause translated code)
public class sqlite extends sqliteopenhelper { private final context mycontext; public sqlitedatabase base; private static string path ="/data/data/" + "com.example.myexample" + "/databases/"; private static string name = "base.sqlite3"; private static string p = path + name; public sqlite(context context){ super(context, ime, null, 1); this.mycontext = context; createdatabase(); } @override public void oncreate(sqlitedatabase base) {} @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {} @override public synchronized void close() { if(base != null) base.close(); super.close(); } public void createdatabase() { boolean exist1 = checkdatabase(); if(exist1){} else { base = this.getreadabledatabase(); base.close(); copydatabase(); } } private boolean checkdatabase() { sqlitedatabase check = null; try { check = sqlitedatabase.opendatabase(p, null, sqlitedatabase.open_readonly); } catch(sqliteexception e) { } if(check != null) { check.close(); } return check != null ? true : false; } private void copydatabase() { inputstream dat = null; try { dat = mycontext.getassets().open(name); } catch (ioexception e) { e.printstacktrace(); } outputstream dat2 = null; try { dat2 = new fileoutputstream(p); } catch (filenotfoundexception e) { e.printstacktrace(); } byte[] buffer = new byte[1024]; int length; try { while ((length = dat.read(buffer))>0) { dat2.write(buffer, 0, length); } } catch (ioexception e) { e.printstacktrace(); } try { dat2.flush(); dat2.close(); dat.close(); } catch (ioexception e) { e.printstacktrace(); } } public void opendatabase() { base = sqlitedatabase.opendatabase(p, null, sqlitedatabase.open_readonly); } public cursor selectsomething(string sql) { base = sqlitedatabase.opendatabase(p, null, sqlitedatabase.open_readonly); cursor cursor = base.rawquery(sql,null); return cursor; }
}
thank help!
as stated in comment answer, code this article
" old, outdated, dreadful (concatenation create file paths?), , problematic",
and appears not first encounter problems it.
also, in same comment same answer, proposed use sqliteassethelper. consider trying it.
Comments
Post a Comment