android中如何将数据库信息保存到文件中?
我有一个使用预先填充的数据库来列出事件的应用程序。该应用程序允许人们通过在 isFavorite 列中设置“1”来将这些事件保存到他们的收藏夹中。然后,用户只能查看“最喜欢的”事件列表,该列表搜索 isFavorite = 1 的所有行。
如果事件发生任何更改,或者我需要向列表中添加更多内容,我必须进行这些更改,然后推送应用程序的更新完全覆盖了表格,清除了他们的最爱。
有什么方法可以在升级时保存他们已设置为收藏夹的所有事件的 id 列表,然后在加载新数据库后,将该列表中的所有 id 设置为 1,这样用户就不会不会丢失他们喜爱的数据吗?
如果有任何其他更好的解决方案来解决这个问题,我将非常感激,这是迄今为止对我来说最大的障碍。
I have an application that uses a pre-polulated database to list events. The app allows people to save these events to their favorites by setting a '1' to the column isFavorite. Then the user can view only a list of 'favorited' events which searches for all rows that have isFavorite = 1.
If any changes happen to the events or I need to add more to the list, I have to make those changes and then push the update to the app which completely writes over the table, clearing out their favorites.
Is there any way that I can, on upgrade, save a list of id's of all the events that they have set to their favorites, then after the new database has been loaded, to set all id's in that list to 1 so the user doesn't lose their favorited data?
If there are any other better solutions to this problem I would really appreciate it, this has been the biggest hurdle for me so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜,你有一个 SQLiteOpenHelper 类?必须扩展此类,然后提供两个函数:
onCreate
(当查询数据库且数据库不存在时调用该函数(通常首先创建数据库))和onUpdate
(当数据库结构应该更新时查询)。onUpdate
方法有一个SQLiteDatabase
-Object 参数,它是您的数据库。然后,您可以查询所需的信息,保存它们,然后创建新的数据库表。之后,您可以将保存的数据插入回数据库。I guess, you have a SQLiteOpenHelper-class? This class must be extended and then provides two functions:
onCreate
(which is called when the Database is queried and it doesn't exist (Normally creates your Database in the first place)) andonUpdate
(which is queried when the Database structure should be updated).The
onUpdate
-method has anSQLiteDatabase
-Object parameter, which is your Database. You can then Query the information you need, save them and then create the new Database-tables. After that, you can insert your saved data back into the Database.您不能在数据库设计中应对这种情况吗?有一个保存 id 的用户收藏夹表。只要这些id不改变,升级就一定不会影响吗?
Can't you cope with thus in your DB design? Have a user favourites table that holds id's. So long as these id's don't change upgrade won't affect it surely?
一种可能的解决方案是备份部分或全部数据库以便稍后恢复。我发现本指南非常方便 http://www.screaming-penguin.com/node/7749
或者,您可以将他们的最爱保存为 SharedPreferences。 http://developer.android.com/reference/android/content/SharedPreferences.html 了解更多信息。
One possible solution is backing up part or all of your database to restore at a later time. I found this guide quite handy http://www.screaming-penguin.com/node/7749
Alternatively, you may save their favorites as a SharedPreferences. http://developer.android.com/reference/android/content/SharedPreferences.html for more information on that.