Update Android Database

Can someone tell me how to update the database in android. I created an application with an embedded database. I changed the version of the database in the manifest and created the on update method. I wanted to check it to see if the database is being updated properly, but when I use the adb command, only -r will allow me to reinstall, but it stores the database. Is there any way to run adb command which will allow me to update the database. Thanks.

+6
android
source share
6 answers

This is how I would handle my updateRow method:

public void updateRow(long rowId, String code, String name) { ContentValues args = new ContentValues(); args.put("code", code); args.put("name", name); db.update(DATABASE_TABLE, args, "_id=" + rowId, null); } 
+7
source share

What worked for me was to change the version number for the database. This is a constant number used by the onCreate () method of your content provider. Something like this works if you are not using a content provider for your database and are not requesting it directly. See source code example here .

+3
source share

Take a look at the following resources and see if they are useful:

Tasty bookmarks
http://delicious.com/tag/android+adb
http://delicious.com/tag/android+database

DevX: creating and using databases in Android
http://www.devx.com/wireless/Article/40842

Android Training: Database Programming
http://learnandroid.blogspot.com/2008/01/android-database.html

Android Database Package Overview
http://developer.android.com/reference/android/database/package-summary.html

+2
source share

What worked for me is to change the version number parameter that I provide to the DBOpenHelper class, which extends SQLiteOpenHelper (the class used to create / read / write / etc ... the Android database). When the version number parameter is incremented, the onUpdate () function is called once for the new value of this parameter.

+1
source share

If during upgrade you mean rebuilding (as during development, when I want to start with a new database every time my application starts), add this code before accessing the database in your application:

 this.getContext().getApplicationContext().deleteDatabase(DATABASENAME); 

OR

Find the path to the database:

 SQLiteHelper sql_database_helper = new SQLiteHelper(this.getContext().getApplicationContext()); File dbFile = getDatabasePath(DATABASENAME); Log.v("XIX","DB FILE PATH:"+dbFile.getAbsolutePath()); 

Then delete your database from the command line:

 > adb devices List of devices attached 3246%$%^$&17 device > adb shell $ su su rm mydatapath 

NOTE. You may not be able to access the su command without breaking the phone

Explanation: If you do not want to follow the recommendations of r1k0 (this is probably the right way to do this if you are updating an already released application), you can simply delete the database with the following adb commands and restart the application, it will call the onCreate method again.

+1
source share

I do not recommend using this method:

 public void updateRow(long rowId, String code, String name) { ContentValues args = new ContentValues(); args.put("code", code); args.put("name", name); db.update(DATABASE_TABLE, args, "_id=" + rowId, null); } 

Your application will be vulnerable to SQL injection, the best way to do it is something like this:

 public void updateRow(long rowId, String code, String name) { ContentValues args = new ContentValues(); args.put("code", code); args.put("name", name); String whereArgs[] = new String[1]; whereArgs[0] = "" + rowId; db.update(DATABASE_TABLE, args, "_id= ?", whereArgs); } 
+1
source share

All Articles