Java 即使在全新安装并重新运行后,也无法将数据库从版本 2 降级到 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24053786/
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-14 09:55:28  来源:igfitidea点击:

Can't downgrade database from version 2 to 1 even after fresh install and re-run

javaandroiddatabasesqlite

提问by Elad Benda2

I'm writing an android app using SQLite DB.

我正在使用 SQLite DB 编写一个 android 应用程序。

I had few experiments and changed the DB version from 1 to 2.

我进行了很少的实验并将数据库版本从 1 更改为 2。

Then my DB schema became stable and because i didn't release the app and it's for my own use

然后我的数据库架构变得稳定,因为我没有发布应用程序,它供我自己使用

I have decided to change the version to 1 again.

我决定再次将版本更改为 1。

I did fresh install and everything worked fine.

我做了全新安装,一切正常。

But then running for the second time throws this error:

但是第二次运行会引发此错误:

06-05 10:03:35.683: E/AndroidRuntime(9010): android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
06-05 10:03:35.683: E/AndroidRuntime(9010):     at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:361)

Why is that, after all I did fresh install and the DB should have been removed as well. No?

为什么会这样,毕竟我做了全新安装并且数据库也应该被删除。不?

How can i change the version to 1 again?

我怎样才能再次将版本更改为 1?

采纳答案by laalto

This exception is thrown on the following conditions:

在以下条件下抛出此异常:

  • The device that you're running the code on has a database file of version 2.
  • The code is requesting version 1 of the database (with a param to SQLiteOpenHelperconstructor)
  • onDowngrade()is not overridden in your code.
  • 您在其上运行代码的设备具有版本 2 的数据库文件。
  • 该代码正在请求数据库的版本 1(带有SQLiteOpenHelper构造函数的参数)
  • onDowngrade()不会在您的代码中被覆盖。

You say the code worked fine the first time after a fresh install. Make sure there's no other code that would bump up the version number of the same database file to 2 again.

你说代码在全新安装后第一次运行良好。确保没有其他代码会再次将同一数据库文件的版本号提高到 2。

回答by CL.

This exception happens when the database needs to be downgraded, but your SQLiteOpenHelper-derived class does not implement the onDowngrade callback.

当数据库需要降级时会发生此异常,但您的SQLiteOpenHelper派生类未实现onDowngrade 回调

回答by guycole

You don't show where the database is created, which is where the database version is normally assigned (i.e. SQLiteOpenHelper ctor).

您没有显示创建数据库的位置,这是通常分配数据库版本的位置(即 SQLiteOpenHelper ctor)。

Is there any chance you have the database on external file system? Unless you follow the naming conventions, these files are not deleted when the application is removed.

您是否有机会在外部文件系统上拥有数据库?除非您遵循命名约定,否则在删除应用程序时不会删除这些文件。

回答by msal

How did you perform the fresh install?

你是如何执行全新安装的?

  • adb install -r? In that case, the data of you app, including the database(s), won't be touched.
  • adb uninstalland adb install? This would be the right way to perform a clean installation. All data will be removed during the uninstalland your database will be freshly created after the first start after the install.
  • adb install -r? 在这种情况下,您的应用程序的数据,包括数据库,将不会被触及。
  • adb uninstalladb install?这将是执行全新安装的正确方法。所有数据都将在被删除uninstall,你的数据库会后第一次启动后新创建的install

But you write that during the first start after the fresh install, it worked. This can only mean that you somewhere still perform a upgrade to version 2.

但是您在全新安装后的第一次启动期间写道,它起作用了。这只能意味着您仍在某处执行升级到版本 2。

回答by Dittimon

Update: Go to app info and > Storage and clear data should worke as well.

更新:转到应用信息和 > 存储和清除数据也应该有效。

One possible reason it worked first time but not second time could be that the database wasn't called in the first run?

第一次工作但第二次没有工作的一个可能原因可能是第一次运行时没有调用数据库?

Also from Marshmallow, Android app data is automatically backed up.for apps targeting 23+ so your app could have gotten the previous database from the cloud without you knowing.

同样来自 Marshmallow,Android 应用程序数据会自动备份。适用于面向 23 岁以上的应用程序,因此您的应用程序可以在您不知情的情况下从云中获取以前的数据库。

Unfortunately, you can't yet delete app data individually. But if you don't mind killing your backed up data for all apps, you can uninstall the app then go to https://myaccount.google.com/dashboardand expand the Android section and delete backed up app data.

不幸的是,您还不能单独删除应用数据。但是,如果您不介意删除所有应用程序的备份数据,则可以卸载该应用程序,然后转到https://myaccount.google.com/dashboard并展开 Android 部分并删除备份的应用程序数据。

After that, you should be able to re-install the app and get a fresh new database.

之后,您应该能够重新安装该应用程序并获得一个全新的数据库。

This worked for me, but do so at your own risk.

这对我有用,但风险自负。

回答by Blasanka

You can override the onDowngrade()by your own if you want to be able to run your application with a database on the device with a higher version than your code can handle.

onDowngrade()如果您希望能够在设备上的数据库版本高于您的代码可以处理的版本上运行您的应用程序,您可以自己覆盖。

This is the default implementation of onDowngrade():

这是的默认实现onDowngrade()

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    throw new SQLiteException("Can't downgrade database from version " +
            oldVersion + " to " + newVersion);
}

If you need to override this method please read this question also.

如果您需要覆盖此方法,请同时阅读此问题

Without override that method, we cannot decrease the SQLite version after increased and executed. So you need to change back to 2or greater than 2:

如果不覆盖该方法,我们将无法在增加和执行后减少 SQLite 版本。所以你需要改回2或大于2

public DatabaseHelper(Context context) {
   super(context, DB_NAME, null, 2);
}

If you are trying to update your table,probably need to pass 3not 2and each and every time you need to update the table, you need to increase the version(not decrease).

如果您正在尝试更新您的表,可能需要通过3not2并且每次需要更新表时,您都需要增加 version(not reduction)

回答by H.Josue

This means that there is already a previous file and you are trying to create another one with the same name, you can change the name of the database, or on the device you are emulating uninstall the application, and emulate it again.

这意味着已经有一个先前的文件并且您正在尝试创建另一个具有相同名称的文件,您可以更改数据库的名称,或者在您正在模拟的设备上卸载应用程序,然后再次模拟它。

回答by Balaji Ramavathu

Had the same problem and I solved it like this.

有同样的问题,我是这样解决的。

@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  db.setVersion(oldVersion);
}

回答by Brahem Mohamed

onDowngrade() is not overridden in your code. You should handle onDownGrade().

onDowngrade() 不会在您的代码中被覆盖。你应该处理 onDownGrade()。

回答by 23shumon

download the sqlite db file from Android DeviceFileExplorer

[data/data/your package name/databases]

open the file using (DB Browser for SQLite)

goto Edit Pragmastab and downgrade the db version from User version

finally save the file and upload it to DeviceFileExplorer

enter image description here

从 Android DeviceFileExplorer

[data/data/your package name/databases]下载 sqlite db 文件,

使用(DB Browser for SQLite)打开文件

转到Edit Pragmas选项卡并从用户版本降级 db 版本,

最后保存文件并将其上传到DeviceFileExplorer

在此处输入图片说明