将 SQLite 数据库从一个版本升级到另一版本?
我从 Logcat 收到错误,指出某个列(在我的 SQLiteOpenHelper 子类中)不存在。我认为我可以通过更改 DATABASE_CREATE 字符串来升级数据库。但显然不是,那么我如何(逐步)将我的 SQLite 数据库从版本 1 升级到版本 2?
如果这个问题看起来“新手”,我深表歉意,但我仍在学习 Android。
@Pentium10 这就是我在 onUpgrade 中所做的:
private static final int DATABASE_VERSION = 1;
....
switch (upgradeVersion) {
case 1:
db.execSQL("ALTER TABLE task ADD body TEXT");
upgradeVersion = 2;
break;
}
...
I am getting an error from Logcat
saying that a certain column (in my SQLiteOpenHelper
subclass) does not exist. I thought I could upgrade the database by changing the DATABASE_CREATE
string. But apparently not, so how can I (step-by-step) upgrade my SQLite Database from version 1 to version 2?
I apologize if the question seems "noobish", but I am still learning about Android.
@Pentium10 This is what I do in onUpgrade:
private static final int DATABASE_VERSION = 1;
....
switch (upgradeVersion) {
case 1:
db.execSQL("ALTER TABLE task ADD body TEXT");
upgradeVersion = 2;
break;
}
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,在遇到更大的问题之前,您应该知道 SQLite 在 ALTER TABLE 命令上受到限制,它只允许
add
和rename
,不允许删除/删除,这是通过重新创建来完成的表的。您应该始终拥有新的表创建查询,并使用它来升级和传输任何现有数据。注意:onUpgrade 方法为您的 sqlite 辅助对象运行一个方法,您需要处理其中的所有表。
所以建议 onUpgrade:
Listcolumns = DBUtils.GetColumns(db, TableName);
ALTER table " + TableName + " RENAME TO 'temp_" + TableName
)columns.retainAll(DBUtils.GetColumns(db, TableName));
)String cols = StringUtils.加入(列,“,”);
)db.execSQL(字符串.format(
"插入 %s (%s) 从 temp_%s 中选择 %s",
表名,列,列,表名));
DROP table 'temp_" + TableName
)。
Ok, before you run into bigger problems you should know that SQLite is limited on the ALTER TABLE command, it allows
add
andrename
only no remove/drop which is done with recreation of the table.You should always have the new table creation query at hand, and use that for upgrade and transfer any existing data. Note: that the onUpgrade methods runs one for your sqlite helper object and you need to handle all the tables in it.
So what is recommended onUpgrade:
if not exists
(we are doing an upgrade, so the table might not exists yet, it will fail alter and drop)List<String> columns = DBUtils.GetColumns(db, TableName);
ALTER table " + TableName + " RENAME TO 'temp_" + TableName
)columns.retainAll(DBUtils.GetColumns(db, TableName));
)String cols = StringUtils.join(columns, ",");
)db.execSQL(String.format(
"INSERT INTO %s (%s) SELECT %s from temp_%s",
TableName, cols, cols, TableName));
DROP table 'temp_" + TableName
).
对于绝大多数情况来说,像下面这样的事情不是更容易吗?只需为每个版本升级添加新列:
有关此内容的更多信息,请查看此 博客。
Wouldn't something like the following be easier for the vast majority of cases? Just add the new column for each version upgrade:
For a bit more on this, check out this blog.
这是我升级数据库的方法。
在我的应用程序的早期版本中,
gameType
列不存在。在新版本中,确实如此。这是复制数据库文件的代码。数据库最初是空的,我在应用程序外部创建了它。 (我使用了一个名为 Navicat for SQLite 的程序。)
Here is how I upgrade my database.
In a previous version of my app, the
gameType
column doesn't exist. In the new version, it does.Here's the code to copy the database file. The database is initially empty, and I created it outside my app. (I used a program called Navicat for SQLite.)