更新数据库 Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2097657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:31:39  来源:igfitidea点击:

Update Database Android

android

提问by user237259

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 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.

谁能告诉我如何在android中更新数据库。我创建了一个带有嵌入式数据库的应用程序。我更改了清单中的数据库版本并创建了 on update 方法。我想测试它以查看数据库是否正确更新,但是当我使用 adb 命令时,只有 -r 允许我重新安装但它保留了数据库。有没有办法运行 adb 命令来更新数据库。谢谢。

回答by hassanadnan

Here is how I would handle my updateRow method:

这是我将如何处理我的 updateRow 方法:

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);
}

回答by r1k0

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.

对我有用的是更改数据库的版本号。这是内容提供者的 onCreate() 方法使用的常数。如果您的数据库不使用内容提供程序,并直接查询它,则类似的东西会起作用。请参阅此处的源代码示例。

回答by Israel ANY

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

美味书签
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

DevX:在 Android 中创建和使用数据库
http://www.devx.com/wireless/Article/40842

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

学习Android:数据库编程
http://learnandroid.blogspot.com/2008/01/android-database.html

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

Android 数据库包汇总
http://developer.android.com/reference/android/database/package-summary.html

回答by jaxvy

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.

对我有用的是更改我提供给 DBOpenHelper 类的版本号参数,该类扩展了 SQLiteOpenHelper(用于创建/读取/写入/等的类...Android 数据库)。当版本号参数递增时,将调用一次 onUpdate() 函数以获取此参数的新值。

回答by tjb

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 sucommand without cracking your phone

注意:如果su不破解手机,您可能无法访问该命令

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.

说明:如果您不想遵循 r1k0 给出的建议(如果您正在升级已发布的应用程序,这可能是正确的做法),那么您可以使用以下 adb 命令删除数据库并重新启动您的应用将再次调用 onCreate 方法。

回答by Jodson Leandro

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:

您的应用程序将容易受到 sql 注入攻击,最好的方法是这样的:

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);
}