替换文档目录中的文件



在我的应用程序的下一个版本中,我对SQLite数据库进行了更改,因为对SQLite的更改,我还对从中读取的一些代码进行了更改——主要是向查询方法添加列。

由于这些更改,下次用户更新应用程序时,必须有新的数据库才能使其工作。在我的应用程序的早期版本中,我有一个名为File.sqlite的数据库,当用户新鲜下载应用程序时,该文件会保存到他们的文档文件夹中。现在,我已经用一个新文件构建了应用程序,但名称相同的file.sqlite中有更多的列等,但当我更新应用程序(不是新安装)时,新文件不会取代旧文件。

如何确保用户更新应用程序时,文件将被替换/更新?

到目前为止,我知道我可以使用以下方法来获取文档目录中旧文件的路径:

//Reference the path to the documents directory.
NSString *documentDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//Get the path of old file.
NSString *filePath = [documentDir stringByAppendingPathComponent:@"File.sqlite"];

新文件是否必须有不同的名称,而我只是在代码中引用新名称?这在我看来很傻,我必须能够复制旧版本并使用新版本?

此外,我甚至不确定这是否是做我正在努力做的事情的最佳方法,如果你知道更好的方法,请提供建议。

您可以检查您的应用程序是新安装的还是来自AppDelegate的更新。然而,你似乎没有将你的应用程序版本保存在NSUserDefaults中,这对你来说会有点棘手。您将新版本的File.sqlite保存在Resource中,并将其复制到文档目录中。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
NSString* curVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString* lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastVersion"];
if ( lastVersion == nil ) 
{
//App Installed for the first time. In your case it might be an update because you do not store lastVersion value.
//Reference the path to the documents directory.
NSString *documentDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//Get the path of old file.
NSString *filePath = [documentDir stringByAppendingPathComponent:@"File.sqlite"];
if([[NSFileManager defaultManager] fileExistsAtPath: filePath])
{
[fileManager removeItemAtPath:filePath error:&error] //Delete old file
}
//Copy New File.sqlite from Resource Bundle.
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"sqlite"];
[fileManager copyItemAtPath:resourcePath toPath:filePath error:&error]; 
} 
else if (![lastVersion isEqual: curVersion]) 
{
// App is updated. For future Versions you can Update files from here! 
} 
[[NSUserDefaults standardUserDefaults] setObject:curVersion forKey:@"LastVersion"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

希望这能有所帮助!

NSArray *pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [pathsToDocuments objectAtIndex: 0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"File.sqlite"]; //File.sqlite old database
NSString *pathStr = [documentsDirectory stringByAppendingPathComponent:@"File1.sqlite"]; // New database file name
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
FMDatabase *database1 = [FMDatabase databaseWithPath:pathStr];
[database1 open];
NSString *str = @"select * from table_name"; // old database fire query for use data value fetch
FMResultSet *res=[database executeQuery:str];
//   Your old database value updated to update new database.
while ([res next]) {
NSString *query= // Write update query for update column value
[database1 executeUpdate:query];
}
[database1 close];
[database close];
// remove old database file 
if ([[NSFileManager defaultManager] isReadableFileAtPath: dbPath])
{
if ([[NSFileManager defaultManager] removeItemAtPath:dbPath error: NULL] != YES)
NSAssert2(0, @"Fail to copy database from %@ in %@", dbPath, pathStr);
}

最新更新