android - How to upgrade SQLDatabase with this code? -
android - How to upgrade SQLDatabase with this code? -
i want upgrade database newer version. how go doing in upgrade method gives you?
private static final string database_name = " nba"; private static final string database_table = "booklist"; private static final int database_version = 1; private static string tag = "upgrading database!"; public static final string key_book = "book"; public static final string key_author = "author"; public static final string key_isbn = "isbn"; public static final string key_rowid = "_id"; public static final string key_rating = "rating"; public static final string key_status = "status"; private databasehelper mdbhelper; private sqlitedatabase mdb; private static final string database_create = " create table " + database_table + " (" + key_rowid + " integer primary key autoincrement, " + key_author + " text not null, " + key_book + " text not null, " + key_rating + " text not null, " + key_status + " text not null, " + key_isbn + " text not null); "; private final context mctx; public dbadapter (context ctx){ this.mctx = ctx; } private static class databasehelper extends sqliteopenhelper{ databasehelper(context context){ super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(database_create); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(tag, "upgrading database version " + oldversion + " " + newversion + ", destroy old data"); db.execsql("drop table if exists user"); db.execsql("drop table if exists site"); db.execsql("drop table if exists site_user"); db.execsql("drop table if exists article"); oncreate(db); } } public dbadapter open() throws sqlexception{ mdbhelper = new databasehelper(mctx); mdb = mdbhelper.getwritabledatabase(); homecoming this; } public void close(){ mdbhelper.close(); } public long createbook(string book, string author, string isbn, float rating, string status){ contentvalues initialvalues = new contentvalues(); initialvalues.put(key_book, book); initialvalues.put(key_author, author); initialvalues.put(key_isbn, isbn); initialvalues.put(key_rating, rating); initialvalues.put(key_status, status); homecoming mdb.insert(database_table, null, initialvalues); } public boolean deletebook(long rowid){ homecoming mdb.delete(database_table, key_rowid + "=" + rowid, null) > 0; } public cursor fetchallbooks(){ homecoming mdb.query(database_table, new string[]{key_book, key_isbn,key_author,key_rowid, key_rating, key_status}, null, null, null, null, null); } public cursor fetchbook(long rowid) throws sqlexception{ cursor mcursor = mdb.query(database_table, new string[]{key_book, key_author,key_rowid, key_isbn, key_rating, key_status}, key_rowid + "=" + rowid, null, null, null, null); if(mcursor != null){ mcursor.movetofirst(); } homecoming mcursor; } public boolean updatebook(long rowid, string book, string author, string isbn, float rating, string status){ contentvalues args = new contentvalues(); args.put(key_book, book); args.put(key_author, author); args.put(key_isbn, isbn); args.put(key_rating, rating); args.put(key_status, status); homecoming mdb.update(database_table, args, key_rowid + "=" + rowid, null)> 0; } }
there's no "magic bullet" upgrading database. strategy current implementation fine except blow away of users data. elegant, yet complex solution, assign version number database creating special table hold value. then, based on version, onupgrade method executes series of alter statements modify construction meet new version. incrementally , iteratively each version needs know how upgrade previous version.
that say, version 3 has no thought how upgrade version 1 2, know how upgrade version 2 3. or, if new version version 5 , you're @ version 1, run script upgrade 1 2, 2 3, 3 4 , 4 5. create sense?
if you're asking how modify table construction in sql, check out alter table statement.
android sql
Comments
Post a Comment