database scheme update

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

database scheme update

Postby swordi » Wed Jun 17, 2009 5:48 pm

hey

is there a way to do a database update on a published app??? i would like to add some columns to my table, but i cant find a good and easy solution to do this

can anybody help me with a link or some hints???

thanks a lot
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Top

Postby nmc » Wed Jun 17, 2009 6:10 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. SQLiteDatabase db;
  3.  
  4. /* open db and check for old scheme */
  5.  
  6. if (needsUpdate)
  7.  
  8.     db.rawQuery("alter table x add column y integer", null);
  9.  
  10.  
Parsed in 0.029 seconds, using GeSHi 1.0.8.4
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby swordi » Wed Jun 17, 2009 6:12 pm

hey

sorry this looks really cool but where comes the needsUpdate variable???

can you please give me more informations about this

thanks
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Postby nmc » Wed Jun 17, 2009 11:46 pm

The 'needsUpdate' comes from the comment section ("open db and check for old scheme") - how you can do that checking depends on your db and code, so I cant tell you w/o seeing your code and/or tables.

Usually I use a table containing one record with a "database version" for this, but maybe you could also use something like "select y from x" after verifying that the table isnt empty and then see if you get an error or at least one row returned.

Hope that helps :-)
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby swordi » Thu Jun 18, 2009 7:45 am

ah ok

i thought its a mechanism from android to do this better. thx for your response
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Postby WarrenFaith » Fri Jun 19, 2009 9:03 am

You can use the database version variable (look for them in the NotePad API-Sample, class NotePadProvider)
If you increase this version number, the following method will be invoked and you can do there your alter commands
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  4.  
  5.     Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  6.  
  7.             + newVersion + ", which will destroy all old data");
  8.  
  9.     db.execSQL("DROP TABLE IF EXISTS notes");
  10.  
  11.     onCreate(db);
  12.  
  13. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests