我的应用程序在App Store上,并在其新版本中更新了架构,如列数已更改,列名称已更改。但是现在,当我在App Store上传新版本时,该应用程序崩溃。如何使用SQLite进行数据库迁移?
我开始知道这是因为我改变了架构。我有使用SQLite所以有这个问题的解决方案吗?
我的应用程序在App Store上,并在其新版本中更新了架构,如列数已更改,列名称已更改。但是现在,当我在App Store上传新版本时,该应用程序崩溃。如何使用SQLite进行数据库迁移?
我开始知道这是因为我改变了架构。我有使用SQLite所以有这个问题的解决方案吗?
SQLite在打开(或创建)数据库时支持'版本'。这是您决定的独特价值。当您更改模式时,应该更改'版本'字符串,以便废弃旧数据库。与第一次设置时创建数据库的方式相同,您还必须手动将任何重要数据从前一个数据库迁移到新数据库。
不幸的是,没有办法将应用程序返回到最后一个好版本,最好的做法是将应用程序从商店中删除几天,直到您找到修复程序。
试试这个。
插入数据从旧的数据库
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:dbName];
BOOL dbSuccess = [fileManager fileExistsAtPath:dbPath];
if (dbSuccess) {
NSDictionary *oldDetailsDict = [NSDictionary dictionaryWithContentsOfFile:writablePath];
if(![[oldDetailsDict valueForKey:@"version"] isEqualToString:Current_APP_VERSION]){
NSString *oldVersion = [oldDetailsDict valueForKey:@"version"];
if ([oldVersion isEqualToString:Current_APP_VERSION]) {
//no code change needed
}else{
//Get data from old db
//Data moved to array
NSArray *oldDataArray = [Product getDataFromDatabase];
//Remove the old Database
NSString *deletePath = [documentsDirectory stringByAppendingPathComponent:@"sample.sqlite"];
[fileManager removeItemAtPath:deletePath error:nil];
//Create the new DB for new version
[self createdatabase];
//Again Insert the old data to new db
for (Product *pro in oldDataArray) {
[pro insertToDB];
}
}
}
}
您的数据库已经存在于旧版本的应用程序得到什么。这就是它崩溃的原因。所以,你应该删除旧的数据库,并通过代码创建这个新的数据库。 – 2012-03-16 09:45:20
@ user1025560你能解释一下它是如何完成的吗? – Developer 2012-03-16 09:47:15