您的位置:首页 > 博客中心 > 数据库 >

拷贝数据库文件到路径

时间:2022-03-14 03:27

NSFileManager *fileManager = [NSFileManager defaultManager];

    NSError *error;

    

    //复制本地数据库文件到安装目录

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentDirectory = [paths objectAtIndex:0];

    //APP安装目录中的document目录路径

    NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"CountryDictionary.db"];

    

    if ([fileManager fileExistsAtPath:dbPath]==NO) {

        //项目中的数据库文件路径

        NSString *resourcePath = [[NSBundle mainBundle]pathForResource:@"CountryDictionary" ofType:@"db"];

        [fileManager copyItemAtPath:resourcePath toPath:dbPath error:&error];

    } else {

        //更新APP Documnet目录的数据库文件,如果存在则先删除再复制最新的数据库文件过去。等本地数据库设计好后,下面的代码需要注释掉。

        NSError *error;

        if ([fileManager removeItemAtPath:dbPath error:&error]!=YES) {

            NSLog(@"unable to delete file %@",[error localizedDescription]);

        }

        

        NSString*resourcePath =[[NSBundle mainBundle] pathForResource:@"CountryDictionary" ofType:@"db"];

        [fileManager copyItemAtPath:resourcePath toPath:dbPath error:&error];

    }

热门排行

今日推荐

热门手游