如果是SQLiteOpenHelper的onCreate()/ onUpgrade()运行?SQLiteOpenHelper、onCreate、onUpgrade

2023-09-11 10:16:44 作者:▲ Memories°怀念

我在创造我的表,我的 SQLiteOpenHelper 的onCreate()但收​​到

I create my tables in my SQLiteOpenHelper onCreate() but receive

SQLiteException: no such table

SQLiteException: no such column

错误。为什么呢?

errors. Why?

(这是几十类似的问题,每个星期,试图提供一个规范社区维基问题/答案在这里,使所有这些问题都可以被定向到一个很好的借鉴。合并后总结)

(This is the amalgamated summary of tens of similar questions every week. Attempting to provide a "canonical" community wiki question/answer here so that all those questions can be directed to a good reference.)

推荐答案

SQLiteOpenHelper onCreate()和onUpgrade()回调是通过调用来调用数据库时竟开,例如getWritableDatabase().创建数据库辅助对象本身时,不打开数据库。

SQLiteOpenHelper onCreate() and onUpgrade() callbacks are invoked when the database is actually opened, for example by a call to getWritableDatabase(). The database is not opened when the database helper object itself is created.

SQLiteOpenHelper 版本的数据库文件。版本号是 INT 传递给constructor.在数据库文件,版本号存储在 PRAGMA user_version

SQLiteOpenHelper versions the database files. The version number is the int argument passed to the constructor. In the database file, the version number is stored in PRAGMA user_version.

的onCreate()仅当数据库文件不存在,刚刚创建运行。如果的onCreate()返回成功(没有抛出异常),假设该数据库将与请求的版本号创建。作为一个暗示,你不应该赶上的SQLException S在的onCreate()自己。

onCreate() is only run when the database file did not exist and was just created. If onCreate() returns successfully (doesn't throw an exception), the database is assumed to be created with the requested version number. As an implication, you should not catch SQLExceptions in onCreate() yourself.

onUpgrade()时数据库文件存在,但存储的版本号低于要求在构造函数中只调用。该 onUpgrade()应更新表架构所请求的版本。

onUpgrade() is only called when the database file exists but the stored version number is lower than requested in constructor. The onUpgrade() should update the table schema to the requested version.

当更改code(的onCreate()),你应该确保更新数据库表模式。两种主要的方法:

When changing the table schema in code (onCreate()), you should make sure the database is updated. Two main approaches:

删除旧的数据库文件,以便的onCreate()再次运行。这通常是$ pferred的发展时刻p $,你必须在安装的版本控制和数据丢失是不是一个问题。有些方法可以删除数据库文件:

Delete the old database file so that onCreate() is run again. This is often preferred at development time where you have control over the installed versions and data loss is not an issue. Some ways to to delete the database file:

卸载应用程序。使用从外壳应用程序管理器或亚行卸载your.package.name

清除应用程序数据。使用应用程序管理器。

Clear application data. Use the application manager.

增加数据库版本,以便 onUpgrade()被调用。这是因为更多的code需要稍微复杂一些。

Increment the database version so that onUpgrade() is invoked. This is slightly more complicated as more code is needed.

有关开发时间模式升级,其中的数据丢失是不是一个问题,你可以使用 execSQL(DROP TABLE IF EXISTS<表名>中)的删除您现有的表和呼叫的onCreate()来重新创建数据库。 安卓存储sqlite之实现

For development time schema upgrades where data loss is not an issue, you can just use execSQL("DROP TABLE IF EXISTS <tablename>") in to remove your existing tables and call onCreate() to recreate the database.

有关发布的版本,你应该实施 onUpgrade数据迁移()让你的用户不会丢失他们的数据。

For released versions, you should implement data migration in onUpgrade() so your users don't lose their data.