SQLiteOpenHelper onCreate()/onUpgrade()何时运行? [英] When does SQLiteOpenHelper onCreate() / onUpgrade() run?

查看:119
本文介绍了SQLiteOpenHelper onCreate()/onUpgrade()何时运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的 SQLiteOpenHelper onCreate()中创建了表,但是收到了

I have created my tables in my SQLiteOpenHelper onCreate() but receive

SQLiteException: no such table

SQLiteException: no such column

错误.为什么?

注意:

(这是每周数十个类似问题的汇总摘要.尝试在此处提供规范"社区Wiki问题/答案,以便将所有这些问题都可以作为参考.)

(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 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 参数.上下文,%20java.lang.String,%20android.database.sqlite.SQLiteDatabase.CursorFactory,%20int%29"rel =" noreferrer>构造函数.在数据库文件中,版本号存储在 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()成功返回(不引发异常),则假定该数据库是使用请求的版本号创建的.这意味着,您不应该自己在 onCreate()中捕获 SQLException .

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 the constructor. The onUpgrade() should update the table schema to the requested version.

在代码中更改表架构( onCreate())时,应确保数据库已更新.两种主要方法:

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

  1. 删除旧的数据库文件,以便再次运行 onCreate().在您可以控制安装的版本并且数据丢失不是问题的开发时间,这通常是首选.删除数据库文件的一些方法:

  1. 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 delete the database file:

  • 卸载应用程序.使用应用程序管理器或从外壳程序 adb卸载your.package.name .

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

Clear application data. Use the application manager.

增加数据库版本,以便调用 onUpgrade().随着需要更多代码,这会稍微复杂一些.

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

  • 对于开发时架构升级而言,数据丢失不是问题,您可以仅使用 execSQL("DROP TABLE IF EXISTS< tablename>")来删除现有表并调用 onCreate()重新创建数据库.

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

这篇关于SQLiteOpenHelper onCreate()/onUpgrade()何时运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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