SQLiteOpenHelper中的onDowngrade()(或降级时不会崩溃的另一种方式) [英] onDowngrade() in SQLiteOpenHelper (or another way to not crash when downgrading)

查看:180
本文介绍了SQLiteOpenHelper中的onDowngrade()(或降级时不会崩溃的另一种方式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java的新手。使用我的应用程序,并决定,我将我的DATABASE_VERSION降低到1(没有真正的原因)。

Very new to Java. Working with my app, and decided, I'd drop my DATABASE_VERSION back down to 1 (no real reason).

当我启动应用程序时,它崩溃了,一个错误是:

When I started the app, it crashed, and one of the errors was:


E / AndroidRuntime(14905):引起:
android.database.sqlite.SQLiteException:可以将数据库从
版本17降级到4

E/AndroidRuntime(14905): Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 17 to 4

E / AndroidRuntime(14905):at
android.database.sqlite.SQLiteOpenHelper.onDowngrade( SQLiteOpenHelper.java:307)

E/AndroidRuntime(14905): at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:307)

我已经有一个 onUpgrade(),在哪里删除表然后运行 onCreate() ...所以我想我会在 >:

I already have an onUpgrade(), where I delete the tables then runs the onCreate()... so I thought I'd make an onDowngrade():

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

但显然这不是我可以覆盖的超类方法。

But apparently that's not a superclass method that I can override.

有没有办法(希望简单的方法)允许我将数据库版本更改为我想要的任何数字并让它在没有立即崩溃的情况下运行?

Is there a way (easy way hopefully) to allow me to change the database version to any number I want and have it still run without crashing immediately?

推荐答案


有没有办法(希望简单的方法)允许我将数据库版本更改为我想要的任何数字并让它仍然存在运行时不会立即崩溃?

Is there a way (easy way hopefully) to allow me to change the database version to any number I want and have it still run without crashing immediately?

在开发过程中,只需通过设置中应用的清除数据按钮清除数据库,或者从设置中卸载应用程序,或者擦除整个模拟器(如果这是一个模拟器)。您的数据库将在下次运行时通过在 SQLiteOpenHelper 中调用 onCreate()重新创建。

In development, simply get rid of your database, either via the Clear Data button for your app in Settings, or by uninstalling the app from settings, or by wiping the whole emulator (if this is an emulator). Your database will be created anew on next run via a call to onCreate() in your SQLiteOpenHelper.

在生产中,正如rciovati所说,支持降级的概念仅在API Level 11中引入,我从未亲自尝试过。

In production, as rciovati noted, the concept of supporting a downgrade was only introduced with API Level 11, and I have never tried it personally.

这篇关于SQLiteOpenHelper中的onDowngrade()(或降级时不会崩溃的另一种方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆