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

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

问题描述

我其实深化发展这是使用 ORMLite库(这是美妙的BTW)的应用程序,但我用它一个初学者。

我有一个关于数据库设备内部的更新的问题。

让我们说别人下载的谷歌播放我的应用程序。几个月后,我肯定会填充新的条目一些表。

当这个人是做应用程序的更新,我怎么能只与我的新条目更新数据库,并保持旧的里面。

要更加清晰,假设用户回答我的应用程序的问题。当我将引入新的问题,如何可以将他们的分贝时,他更新我的应用程序的并保留问题那些已经有了答案?

感谢

解决方案
  

当这个人是做应用程序的更新,我怎么能只与我的新条目更新数据库,并保持旧的里面。

的想法是使用传递到 onUpgrade(...)方法的版本号。随着 ORMLite ,在 OrmLiteSqliteOpenHelper.onUpgrade(...)方法采用 oldVersion NEWVERSION 号。然后,您可以编写转换code到应用程序,它能够将数据从旧的格式转换和更新的模式。

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

  

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

要报价,你可以做类似如下:

 如果(oldVersion 2){
  //我们增加了年龄列在第2版
  dao.executeRaw(ALTER TABLE`account` ADD COLUMN年龄整数);
}
如果(oldVersion 3;){
  //我们增加体重列第3版
  dao.executeRaw(ALTER TABLE`account` ADD列重整数);
}
 

如果您有现成的,你需要转换,然后如果可能的话,你应该做的转换中的SQL数据。

另一种方法是有一个帐户实体和 OldAccount 实体指向同一个表名。然后你就可以在 OldAccount 实体使用 oldAccountDao ,将它们转换为帐户读实体,然后使用 accountDao 返回到同一个表进行更新。你必须要小心这里的对象缓存。

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

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

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 insert them in the db when he updates my app and keep the questions that have already been answered ?

Thanks

解决方案

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.

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.

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.

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天全站免登陆