ORMLite更新数据库 [英] ORMLite update of the database

查看:435
本文介绍了ORMLite更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上正在开发一个使用ORMLite库(这是美妙的btw)的应用程序,但我是一个初学者使用它。

I'm actually developing an app which is using ORMLite library (which is wonderful btw) but I'm a beginner using it.

我有一个问题更新设备中的数据库。

I have a question about the update of the database inside the device.

假设有人在Google Play上下载了我的应用程序。几个月后,我肯定会填充一些表与新的条目。

Let's say that someone download my app on the Google Play. Few months later I will certainly populate some tables with new entries.

当这个人在更新应用程式时,如何使用新的项目更新资料库,并保留旧的资料。

When this person is doing an update of the app, how can I just update the database with my new entries and keep the old ones inside it.

为了更清楚,假设用户在我的应用程序中回答了问题。当我要介绍新问题时,如何在更新我的应用并保留已回答的问题时将其插入数据库?

To be more clear, imagine that the user answered questions in my app. When I will introduce new questions, how can I insert them in the db when he updates my app and keep the questions that have already been answered ?

谢谢

推荐答案


当这个人在更新应用程式时,

When this person is doing an update of the app, how can I just update the database with my new entries and keep the old ones inside it.

这个想法是使用传递给它的版本号方法 onUpgrade(...)。使用 ORMLite OrmLiteSqliteOpenHelper.onUpgrade(...)方法使用 oldVersion newVersion 数字。然后,您可以将转换代码写入您的应用程序,以便能够转换旧格式的数据并更新模式。

The idea is to use the version number that is passed to the onUpgrade(...) method. With ORMLite, the OrmLiteSqliteOpenHelper.onUpgrade(...) method takes an oldVersion and newVersion number. You then can write conversion code into your application that is able to convert the data from the old format and update the schema.

有关详细信息,请参阅ORMLite文档主题:

For more information, see the ORMLite docs on the subject:


http:// ormlite。 com / docs / upgrade-schema

要引用,您可以执行以下操作:

To quote, you could do something like the following:

if (oldVersion < 2) {
  // we added the age column in version 2
  dao.executeRaw("ALTER TABLE `account` ADD COLUMN age INTEGER;");
}
if (oldVersion < 3) {
  // we added the weight column in version 3
  dao.executeRaw("ALTER TABLE `account` ADD COLUMN weight INTEGER;");
}

如果您有现有的数据需要转换,

If you have existing data that you need to convert then you should do the conversions in SQL if possible.

另一种方法是使用 Account 实体和 OldAccount 实体指向同一个表名。然后,您可以使用 oldAccountDao 读取 OldAccount 实体,将它们转换为帐户实体,然后使用 accountDao 更新它们到同一个表。您需要在这里对对象缓存小心。

Another alternative would be to have an Account entity and an OldAccount entity that point to the same table-name. Then you can read in OldAccount entities using the oldAccountDao, convert them to Account entities, and then update them using the accountDao back to the same table. You need to be careful about object caches here.

这篇关于ORMLite更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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