java - Android Sqlite - "No Such Table" Error -
i trying learn sqlite databases, hate dealing back-end stuff, passion. i'm hitting walls seemingly simple problem.
here code think matters databasehelper class
public class databasehelper extends sqliteopenhelper { private static final string database_name = "library"; public static final string table_name = "books"; public static final string title = "title"; public static final string author = "author"; public static final string isbn = "isbn"; public databasehelper(context context) { super(context, database_name, null, 1); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table " + table_name + " (id integer primary key autoincrement, title text, author text, isbn text)"); } public boolean insertbook(string title, string author, string isdn) { try { sqlitedatabase db = getwritabledatabase(); contentvalues cv = new contentvalues(); cv.put(title, title); cv.put(author, author); cv.put(isbn, isdn); db.insert(table_name, null, cv); db.close(); return true; } catch (exception exp) { exp.printstacktrace(); return false; } }
}
and code in main activity
dbhelper = new databasehelper(this); dbhelper.insertbook("harry potter", "jk", "1000"); dbhelper.insertbook("hamlet", "shakespeare", "500");
eclipse telling me there error in insertbook() method. says there "no such table books: ...". have no idea doing wrong here. makes more frustrating couple of minutes before working perfectly, (i think) dropped table , create again whatever reason, though code has not changed since first created (i think...).
any appreciated.
there older version of database on device, have (empty) database in place, not books table. if that's option you, uninstall , reinstall app.
later, when you'd add new table database during production on end-user devices, keep existing data, designated hook add new tables, alter schema or upgrade data onupgrade
method of sqliteopenhelper
.
Comments
Post a Comment