“用于创建数据库的副本…”
BOOL success;NSFileManager *fileManager = [NSFileManager defaultManager];NSError *error;NSArray *paths = NSSearchPathForDirectoriesInDomains(NSdocumentDirectory, NSUserDomainMask, YES);NSString *documentPath = [paths objectAtIndex:0];NSString *finalPath = [documentPath stringByAppendingPathComponent:@"test.sqlite"];success = [fileManager fileExistsAtPath:finalPath];if(success){ NSLog(@"Database Already Created."); return;}NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.sqlite"];success = [fileManager copyItemAtPath:defaultPath toPath:finalPath error:&error];if(success){ NSLog(@"Database Created Successfully.");}
“对于打开该数据库…”
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSdocumentDirectory, NSUserDomainMask, YES);NSString *documentPath = [paths objectAtIndex:0];NSString *finalPath = [documentPath stringByAppendingPathComponent:@"test.sqlite"];if(sqlite3_open([finalPath UTF8String], &database) != SQLITE_OK){ sqlite3_close(database); NSLog(@"Error to Open Database :- %s",sqlite3_errmsg(database));}
“用于在应用程序路径中创建文件夹”
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSdocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folderNSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/FolderName"];if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){ [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];}
“用于在应用程序中复制文件和文件夹”
NSFileManager *fileManager = [NSFileManager defaultManager];NSError *error;NSArray *paths = NSSearchPathForDirectoriesInDomains(NSdocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folderNSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/FolderName"];if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){ BOOL successs; NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Files/FolderName"]; successs = [[NSFileManager defaultManager] fileExistsAtPath:defaultPath]; if(successs) { NSLog(@"TRUE"); NSString *strFile = [NSString stringWithFormat:@"%@",dataPath]; NSLog(@"File :- '%@'",strFile); [fileManager copyItemAtPath:defaultPath toPath:strFile error:&error]; } else NSLog(@"FALSE");}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)