sqlite3 - Android creates database with old schema -
there new sqliteopenhelper:
public class dbmanager extends sqliteopenhelper { public static final int database_version = 2; // database creation sql statement // indexes should not used on small tables. © tutorials point private static final string database_create = "create table " + laws_table_name + "(" + column_law_id + " integer primary key, " + column_law_name + " text not null, " + column_law_text + " text not null, " + column_law_country + " text not null, " + column_law_version_date + " text not null);" + column_law_update_date + " text not null);" + "create table " + chapters_table_name + "(" + column_chapter_id + " integer primary key, " + column_chapter_title + " text not null, " + column_chapter_order + " integer not null, " + column_chapter_weight + " integer not null, " + column_chapter_law + " integer not null, " + column_chapter_text + " text);"; public dbmanager(context context) { super(context, database_name, null, database_version); } // method called during creation of database @override public void oncreate(sqlitedatabase database) { database.execsql(database_create); } // method called during upgrade of database, @override public void onupgrade(sqlitedatabase database, int oldversion, int newversion) { database.execsql("drop table if exists " + laws_table_name + ";"); database.execsql("drop table if exists " + chapters_table_name + ";"); oncreate(database); } public boolean savelawtodatabase(law law) { try { sqlitedatabase sdb = this.getwritabledatabase(); cursor cursor = sdb.rawquery("select * " + laws_table_name + " " + column_law_id + "=" + law.getid(), null); if (cursor.movetofirst()) { deletelaw(law.getid()); } contentvalues newvalues = new contentvalues(); newvalues.put(dbmanager.column_law_id, law.getid()); newvalues.put(dbmanager.column_law_country, law.getcountrycode()); newvalues.put(dbmanager.column_law_version_date, law.getupdatedateasstring()); newvalues.put(dbmanager.column_law_text, law.gettitle()); newvalues.put(dbmanager.column_law_name, law.gettitle()); sdb.insert(dbmanager.laws_table_name, null, newvalues); (chapter chapter:law.getchapters()) { newvalues = new contentvalues(); newvalues.put(dbmanager.column_chapter_id , chapter.getid()); newvalues.put(dbmanager.column_chapter_title , chapter.gettitle()); newvalues.put(dbmanager.column_chapter_order , chapter.getorder()); newvalues.put(dbmanager.column_chapter_weight , chapter.getweight()); newvalues.put(dbmanager.column_chapter_law , law.getid()); newvalues.put(dbmanager.column_chapter_text, chapter.gettext()); sdb.insert(dbmanager.chapters_table_name, null, newvalues); } return true; } catch (exception e) { e.printstacktrace(); return false; } } }
after full reinstalling of application creates db old schema (without "chapters" table). after calling sdb.insert chapters gives me following traceback:
e/sqlitedatabase﹕ error inserting text= title=da test title weight=2 law_id=1 order=2 _id=2 android.database.sqlite.sqliteexception: near "order": syntax error (code 1): , while compiling: insert chapters(text,title,weight,law_id,order,_id) values (?,?,?,?,?,?) @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:889) @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:500) @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:588) @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:58) @ android.database.sqlite.sqlitestatement.<init>(sqlitestatement.java:31) @ android.database.sqlite.sqlitedatabase.insertwithonconflict(sqlitedatabase.java:1475) @ android.database.sqlite.sqlitedatabase.insert(sqlitedatabase.java:1347) @ kg.zuber.pocketlawyer.utils.dbmanager.savelawtodatabase(dbmanager.java:106) @ kg.zuber.pocketlawyer.network.datareceiver$5.onresponse(datareceiver.java:84) @ kg.zuber.pocketlawyer.network.datareceiver$5.onresponse(datareceiver.java:79) @ com.android.volley.toolbox.jsonrequest.deliverresponse(jsonrequest.java:65) @ com.android.volley.executordelivery$responsedeliveryrunnable.run(executordelivery.java:99) @ android.os.handler.handlecallback(handler.java:730) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:213) @ android.app.activitythread.main(activitythread.java:5225) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:741) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:557) @ dalvik.system.nativestart.main(native method)
where wrong?
the problem have 2 sql statements in database_create
statement.
if @ the documentation execsql()
, can see not supported:
multiple statements separated semicolons not supported.
just separate out each table creation statement:
private static final string database_create_laws = "create table " + laws_table_name + "(" + column_law_id + " integer primary key, " + column_law_name + " text not null, " + column_law_text + " text not null, " + column_law_country + " text not null, " + column_law_version_date + " text not null);" + column_law_update_date + " text not null);"; private static final string database_create_chapters = "create table " + chapters_table_name + "(" + column_chapter_id + " integer primary key, " + column_chapter_title + " text not null, " + column_chapter_order + " integer not null, " + column_chapter_weight + " integer not null, " + column_chapter_law + " integer not null, " + column_chapter_text + " text);";
and execute both statements in oncreate()
method:
@override public void oncreate(sqlitedatabase database) { database.execsql(database_create_laws); database.execsql(database_create_chapters); }
Comments
Post a Comment