如何在不丢失数据的情况下复制 Cocoa 中 Application Support 目录中的数据库文件?
How to copy database file in Application Support directory in Cocoa without losing data?
我试过这段代码,但它会在文档目录中创建 BLANK
database.sqlite
文件。
- (void)createEditableCopyOfDatabaseIfNeeded
{
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"prediction.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"prediction.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
如何确保在复制数据库文件时不会创建空数据库?
如有任何帮助,我们将不胜感激。
首先,您的代码并未尝试创建数据库文件。
所以...您只想将数据库文件“prediction.sqlite”从源包复制到 UserDomain 目录?
如果这是你的目的,你要注意代码:
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
正确的应该是:
if (!success) return;
// The writable database does not exist, so copy the default to the appropriate location.
不保证NSSearchPathForDirectoriesInDomains
返回的目录存在;如有必要,您需要自己创建它们(参见 documentation)。 NSFileManager 的 createDirectoryAtPath:withIntermediateDirectories:attributes:error:
可以提供帮助。@Anomie
尝试这些代码:(在复制 "prediction.sqlite" 文件之前,您需要将该文件添加到您的项目包中。):
- (void)createEditableCopyOfDatabaseIfNeeded
{
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"prediction.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (!success){
// create the directories, because the directories returned by NSSearchPathForDirectoriesInDomains are not guaranteed to exist
if (![fileManager createFileAtPath:writableDBPath contents:nil attributes:nil]) {
return;
}
}
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"prediction.sqlite"];
// remove the same file, before you copy the file
[fileManager removeItemAtPath:writableDBPath error:nil];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
我试过这段代码,但它会在文档目录中创建 BLANK
database.sqlite
文件。
- (void)createEditableCopyOfDatabaseIfNeeded
{
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"prediction.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"prediction.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
如何确保在复制数据库文件时不会创建空数据库?
如有任何帮助,我们将不胜感激。
首先,您的代码并未尝试创建数据库文件。
所以...您只想将数据库文件“prediction.sqlite”从源包复制到 UserDomain 目录? 如果这是你的目的,你要注意代码:
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
正确的应该是:
if (!success) return;
// The writable database does not exist, so copy the default to the appropriate location.
不保证NSSearchPathForDirectoriesInDomains
返回的目录存在;如有必要,您需要自己创建它们(参见 documentation)。 NSFileManager 的 createDirectoryAtPath:withIntermediateDirectories:attributes:error:
可以提供帮助。@Anomie
尝试这些代码:(在复制 "prediction.sqlite" 文件之前,您需要将该文件添加到您的项目包中。):
- (void)createEditableCopyOfDatabaseIfNeeded
{
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"prediction.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (!success){
// create the directories, because the directories returned by NSSearchPathForDirectoriesInDomains are not guaranteed to exist
if (![fileManager createFileAtPath:writableDBPath contents:nil attributes:nil]) {
return;
}
}
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"prediction.sqlite"];
// remove the same file, before you copy the file
[fileManager removeItemAtPath:writableDBPath error:nil];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}