在iOS中启动应用程序时如何复制sqlite数据库?

每当我启动应用程序时,我想将数据库中的sqlite数据库从最新更新复制到我的iOS应用程序.

有什么办法吗?

解决方法

你可以添加以下方法给你的appdelegate
- (void) copyDatabaseIfNeeded {

    //Using NSFileManager we can perform many file sy@R_502_6063@ operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    Nsstring *dbPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath];

    if(!success) {

       Nsstring *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
       success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

       if (!success)
          NSAssert1(0,@"Failed to create writable database file with message '%@'.",[error localizedDescription]);
    }
}

- (Nsstring *) getDBPath
{   
    //Search for standard documents using NSSearchPathForDirectoriesInDomains
    //First Param = Searching the documents directory
    //Second Param = Searching the Users directory and not the Sy@R_502_6063@
    //Expand any tildes and identify home directories.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    Nsstring *documentsDir = [paths objectAtIndex:0];
    //NSLog(@"dbpath : %@",documentsDir);
    return [documentsDir stringByAppendingPathComponent:@"database.sqlite"];
}

并在你的完成后用调用方法调用这个方法[self copyDatabaseIfNeeded];希望这将有所帮助.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...