android - Upgrading my SQLite db -
i quite new sqlite.
how upgrade db?
my previous database version had 7 questions in it.
today added 13 new ones, making them 20 in total.
added them using addquestion method.
however, not work.
i have been tinkering onupgrade.
doing wrong.
public class quizhelper extends sqliteopenhelper { private static final int database_version = 1; private static final string database_name = "creativequestion"; private static final string table_quest = "quest"; private static final string key_id = "qid"; private static final string key_quest = "question"; private sqlitedatabase dbase; public quizhelper(context context) { super( context, database_name, null, database_version ); } @override public void oncreate(sqlitedatabase db) { dbase = db; string sql = "create table if not exists " + table_quest + " ( " + key_id + " integer primary key autoincrement, " + key_quest + " text)"; db.execsql( sql ); addquestion(); } private void addquestion() { questionfaci q1 = new questionfaci( "other uses: name other uses of hammer. \n\n example: stir soup." ); this.addquestion(q1); questionfaci q2 = new questionfaci( "rhymes: words rhymes rice. \n\n example: ice" ); this.addquestion(q2); questionfaci q3 = new questionfaci( "with: can cook eggs with... \n\n example: piece of plywood" ); this.addquestion(q3); questionfaci q4 = new questionfaci( "without: can wash clothes without... \n\n example: aunt" ); this.addquestion(q4); questionfaci q5 = new questionfaci( "i will: if bill gates, will... \n\n example: buy spaceship" ); this.addquestion(q5); questionfaci q6 = new questionfaci( "create movie title: night \n\n example: remember" ); this.addquestion(q6); questionfaci q7 = new questionfaci( "other names: other names of cow \n\n example: milk giver" ); this.addquestion(q7); questionfaci q8 = new questionfaci( "other uses: name other uses of cowboy boots. \n\n example: pound nail." ); this.addquestion(q8); questionfaci q9 = new questionfaci( "rhymes: bake a. \n\n example: flake." ); this.addquestion(q9); questionfaci q10 = new questionfaci( "i will: drive helicopter \n\n example: eyes closed" ); this.addquestion(q10); questionfaci q11 = new questionfaci( "create title: greatest \n\n example: heist" ); this.addquestion(q11); questionfaci q12 = new questionfaci( "change ingredient: --- cookie \n\n example: orange chip" ); this.addquestion(q12); questionfaci q13 = new questionfaci( "finish me: can remember times when.. \n\n example: push me on cliff" ); this.addquestion(q13); questionfaci q14 = new questionfaci( "other names: ball bearings \n\n example: mommy's new bangles" ); this.addquestion(q14); questionfaci q15 = new questionfaci( "finish me: donut rolls \n\n example: down hill" ); this.addquestion(q15); questionfaci q16 = new questionfaci( "rhymes: flip \n\n example: clip" ); this.addquestion(q16); questionfaci q17 = new questionfaci( "other names: police \n\n example: civilian peacekeeper" ); this.addquestion(q17); questionfaci q18 = new questionfaci( "create title: go go \n\n example: changin" ); this.addquestion(q18); questionfaci q19 = new questionfaci( "i will: distribute screwdrivers \n\n example: drivers" ); this.addquestion(q19); questionfaci q20 = new questionfaci( "create dish: chicken --- soup \n\n example: dumpling" ); this.addquestion(q20); } @override public void onupgrade(sqlitedatabase db, int oldv, int newv) { string upgradequery = "alter table quest add column question text"; if(newv>oldv) db.execsql( upgradequery ); oncreate( db ); } private void addquestion(questionfaci quest) { contentvalues values = new contentvalues( ); values.put(key_quest, quest.getquestion()); dbase.insert( table_quest, null, values ); } public list<questionfaci> getallquestions(){ list<questionfaci> queslist = new arraylist<questionfaci>( ); string selectquery = "select * " + table_quest; dbase = this.getreadabledatabase(); cursor cursor = dbase.rawquery( selectquery, null ); if (cursor.movetofirst()){ do{ questionfaci quest = new questionfaci( ); quest.setid( cursor.getint( 0 ) ); quest.setquestion( cursor.getstring( 1 ) ); queslist.add(quest); } while (cursor.movetonext()); } return queslist; } }
you should increase database_version
variable example 2. call onupgrade
method.
it's not best approach can drop table if version of current database higher , call oncreate()
.
@override public void onupgrade(sqlitedatabase db, int oldv, int newv) { string upgradequery = "drop table if exists quest"; db.execsql( upgradequery ); oncreate( db ); }
Comments
Post a Comment