Update Database Android
Can anyone tell me how to update the database in android. I created an app with an embedded database. I changed the version of the database in the manifest and created the on update method. I wanted to开发者_StackOverflow社区 test it to see if the database was updating properly but when I use the adb command only the -r will allow me to do a reinstall but it keeps the database. Is there a way to run the adb command that will allow me to update the database. Thanks.
Here 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);
}
What worked for me is changing the version number for the database. This is the constant number that is used by the onCreate() method of your content provider. Something similar works if you don't use a content provider for your database, and query it directly. See source code example here.
Have a look at the following resources and see if they're helpful:
Delicious 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
Learning Android: Database Programming
http://learnandroid.blogspot.com/2008/01/android-database.html
Android Database Package Summary
http://developer.android.com/reference/android/database/package-summary.html
What worked for me is changing the version number parameter that I provide to the DBOpenHelper class which extends the SQLiteOpenHelper (the class used for creating/read/writing/etc... the Android database). When the version number parameter is incremented, the onUpdate() function is called once for the new value of this parameter.
If by updating you mean rebuilding (as I do during development when I want to start with a fresh database every time my application starts) then add this code before you access the database in your application:
this.getContext().getApplicationContext().deleteDatabase(DATABASENAME);
OR
Find the path to your 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 prompt:
> 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 cracking your phone
Explanation: If you don't want to follow the advice given by r1k0 (which is probably the right way to do it if you are upgrading an already release app) then you can just delete the database with the following adb commands and a restart of your app will call the onCreate method again.
I don't advise to use this way:
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 app will be vulnerable to sql injection attack, the best way to do that 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);
}
精彩评论