将内容更新/插入到iPhone应用程序更新上的现有Sqlite数据库中 [英] Update/Insert content into an existing Sqlite db on an iPhone app update

查看:49
本文介绍了将内容更新/插入到iPhone应用程序更新上的现有Sqlite数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里经历了其他类似性质的问题,但是它们都与在应用程序更新中替换以前的sqlite数据库有关.我一直在为所有以前的应用程序执行此操作,因为它们本质上是引用的,并且只需替换即可.

I've gone through other questions of the similar nature here but they are all related to replacing the previous sqlite db on app update. I've been doing that for all the previous apps, since they were referential in nature, and a simple replace sufficed.

我正在使用的这个特定应用是一个测验游戏,需要保持分数.目前大约有15个问题,共有4个级别,在以后的更新中,这些级别可能会增加.由于我以前从未做过此事,所以我很好奇,并打算第一次以正确的方式来做.所以这是我的查询:

This particular app I am working with is a quiz game, and the score needs to be maintained. There are 4 levels with around 15 questions at the moment, and on a later update these might increase. Since I have never done this before, I am curious and intend to do this the right way the first time. So here are my queries:

  1. SO专家推荐的更新内容的首选方法是什么?是否具有版本号(也许在数据库本身中),并且在首次运行新应用程序时,会将新内容插入表中?
  2. 存储插入查询的首选方式是什么?是否应该在实现文件中对它们进行硬编码?

PS.我将数据库复制到Documents目录,因此它在应用程序更新时仍然存在.

PS. I copy the database to the Documents directory, so it persists on app updates.

我应该添加的是,测验应用程序是一种徽标测验"副本,其中需要保留所有问题的分数(或将被回答/未回答的问题状态).因此,第一个版本中有60个问题,随着用户回答它们的状态也会改变.

I should have added that the quiz app is sort of "logos quiz" replica, in which scores (or status of the question which would be answered/unanswered) of all the questions need to be maintained. So there are 60 questions in the first version, and their statuses are changed as the user answers them.

这是我最关心的表的结构,问题表:

EDIT 2: This is structure of the Table I am most concerned about, the Questions Table:

_id(整数,主键),levelId(整数),QuestionImage(变量),CorrectAnswer(变量),boolAnsweredCorrectly(整数).

_id (Integer, PRIMARY Key), levelId (Integer), QuestionImage (Varchar), CorrectAnswer(Varchar), boolAnsweredCorrectly(Integer).

测验就像徽标测验.有一张图片,用户必须输入答案.如果键入的答案与数据库中的正确答案匹配,则将boolAnsweredCorrectly设置为true.我主要关心的是boolAnsweredCorrectly(根据结果为0或1).

The quiz is like Logos Quiz. There's an image and the user has to type the answer. If the answer typed matches the correctanswer in DB, the boolAnsweredCorrectly is set to true. My main concern is boolAnsweredCorrectly (which would be 0 or 1 depending on the result).

当我谈到在应用程序更新时插入数据时,我的意思是.假设第一个版本中有60个问题.在第二个版本中,添加了40个新问题.因此,当用户将应用更新为第二版时,需要将新的40个问题插入问题表中.添加方式应使前60个问题不被弄乱并保持完整.

EDIT 3: When I speak of Inserting data on update of the app, I mean this. Suppose there were 60 questions in the first version. In the second version 40 new questions are added. So, when the user updates the app to the second version, the new 40 questions need to be inserted into the questions table. This is to be added in such a way that the previous 60 questions are not messed up and remain intact.

推荐答案

SO专家推荐的更新内容的首选方法是什么?是否具有版本号(也许在数据库本身中),并且在首次运行新应用程序时,会将新内容插入表中?

What is the preferred way to update the content that the Gurus of SO recommend? Is it to have a version number (perhaps in the DB itself) and upon first run of the new app, the new content is inserted in the table?

取决于您是否仅使用数据库来存储该人的答案的结果(在这种情况下,除了存储该人的测验答案外,您可能不需要对表进行任何插入"操作)或是否有一些单独的表来存储原始问题(如果您不通过某些服务器界面发送新问题,这可能很有意义).

Depends upon whether you're using your database just to store the results of the person's answers (in which case, you probably don't need to do any "inserting" into the table other than storing the person's quiz responses as they happen) or whether you have some separate tables storing the original questions, too (which probably makes sense if you're not delivering new questions via some server interface).

我个人使用configuration表中的database_version字段,格式为"major.minor.revision",其中majorminor假定需要数据库更新的数据库更改,而revision更改则不会).但是我使用这个数据库版本号系统来知道是否要将捆绑软件的数据库重新复制到Documents中(但是我这样做仅是为了对数据库进行结构更改,并且更愿意通过服务器接口获取新数据,但这取决于解决方案的设计方式).如果您确实以编程方式确定要再次复制数据库,但是如果需要保留任何数据(直到Retterdesdialogs),您可能希望将任何数据从旧数据库保存到新数据库中.

I personally use a database_version field in a configuration table with the format of "major.minor.revision", where major or minor presuppose database changes that require database update, whereas revision changes don't). But I use this database version number system to know whether I want to recopy my bundle's database into Documents or not (but I do this for only structural changes to my database, and prefer to get new data via server interface, but this depends upon how your solution is designed). If you do programmatically determine that you want to copy the database again, if you have any data that needs to be preserved, though, to Retterdesdialogs' point, you might want to save any data from your old database into the new database.

存储插入查询的首选方式是什么?是否应该在实现文件中对它们进行硬编码?

What is the preferred way to store the insert queries? Should they be hardcoded with in an implementation file?

无论如何,我认为您不应该有大量的硬编码INSERT语句,因此这不是问题.我从下面的评论中收集到,您期望根据用户以前的版本尝试插入新的问题记录.

I don't think you should have a ton of hardcoded INSERT statements, anyway, so this wouldn't be an issue. I gather from your comment below that you anticipate trying to insert new question records based upon the what version the user previously had.

我个人更倾向于

  • 将最新和最重要的问题放入捆绑软件的数据库中
  • 已根据应用程序的预期检查Documents数据库版本的版本
  • 如果不同(a)保存用户的旧答案; (b)将新数据库从捆绑软件复制到Documents; (c)根据旧数据库的答案更新新数据库中的答案.
  • put your latest and greatest questions in a database in your bundle
  • have app check version of the Documents database version against what the app was expecting,
  • if different (a) save the user's old answers; (b) copy the new database from the bundle to Documents; and (c) update the answers in the new database based upon the old database's answers.

您当然可以通过编程的方式插入记录,但是想像一下当您进入应用程序的第20版时生活会是什么样子,并且您将不得不基于之前的内容对要插入的内容拥有大量的条件逻辑.该应用程序的版本为(因为您不能假设用户将始终具有以前的版本...他们可能是过时的几个版本).当您将每个版本都考虑为添加,删除和修改问题的组合时,它变得更加毛茸茸.

You certainly could do something programmatically inserting records, but just imagine what life will be like when you get to the 20th release of your app and you'll have to have tons of conditional logic about what to insert based upon what the prior version of the app was (because you can't assume the user will always have the previous version ... they could be a few versions out of date). And it gets even hairier when you contemplate each version being a combination of adding, removing, and modifying of questions.

最重要的是,我倾向于使用当前问题的数据库,如果需要的话,可以将其复制到Documents,只需确保应用程序是否具有包含旧答案的旧数据库,然后手动保存它们即可. (或者您可以将答案存储在单独的数据库中.)

Bottom line, I'd lean towards a database of the current questions, copying it over to Documents if you have to, just making sure if the app has old database with old answers, then manually preserve them. (Or you could store the answers in a separate database.)

或者,我怀疑这超出了您的期望,但您可能需要考虑使用 Core Data (首选的iOS持久存储模型).看来已经解决了

Alternatively, and I suspect that this is more than you want to bite off, but you might want to consider using Core Data, the preferred iOS persistent storage model. It appears that it has solved this migration issue as outlined in the Core Data Model Versioning and Data Migration. I've never use Core Data's migration/versioning stuff (I historically have always rolled my own), but it looks promising.

这篇关于将内容更新/插入到iPhone应用程序更新上的现有Sqlite数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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