Tuesday 15 January 2013

android - Keeping data after update sqlite version -



android - Keeping data after update sqlite version -

i had 1 table in sqlite database. changed database version 2 , add together table. user info first table disappeared. asking you: how can maintain user data?

public class database extends sqliteopenhelper { private static final long serialversionuid = 1l; static int databaseversion = 2; static string databasename = "dbinformation"; string table1 = "create table people ( " + "id integer primary key autoincrement, " + "name text, "+ "age integer )"; string table2 = "create table piece of furniture ( " + "id integer primary key autoincrement, " + "type text, "+ "price integer )"; public database(context context) { super(context, databasename, null, databaseversion); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub db.execsql(table1); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql(table2); db.execsql("drop table if exists people"); this.oncreate(db); } }

how can maintain user data?

don't drop people table. create furniture table:

@override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { if (oldversion<2) { db.execsql(table2); } }

tomorrow, might bump schema version 3 , add together yet table:

@override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { if (oldversion<2) { db.execsql(table2); } if (oldversion<3) { db.execsql(create_rutabaga_table_statement); } }

and on.

android sqlite

No comments:

Post a Comment