Ошибка приложения при попытке создания базы данных
Я создал базу данных sql, используя "SQLite Database Browser", перетащил ее в проект Xcode и создал приложение. Он отлично работает на симуляторе, но падает на iPhone с этой ошибкой:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Failed to create writable database file with message 'The operation could‚
not be completed. (Cocoa error 260.)'.'
Здесь мой код:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Creates a writable copy of the bundled default database in the application Documents directory:
NSLog(@"AppDelegate...Looking for embedded Database file...");
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
// Grab the path to the Documents folder:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"users.sql"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) {
NSLog(@"Database File Exists in Documents folder!");
NSLog(@"Its path is: %@", writableDBPath);
return YES;
}
else {
// But if the writable database does not exist, copy the default to the appropriate location.
NSLog(@"!!NO Database File Exists in Documents folder!");
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"users.sql"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
else
NSLog(@"WROTE THE DATABASE FILE!!!");
}
return YES;
}
Опять же, это работает на Simulator, но не на iPhone. (Это не могло иметь ничего общего с файлом с расширением ".sql", а не с расширением ".sqlite", не так ли? Потому что расширения, которые "SQLite Database Browser" предоставляет созданным им файлам..)
Ответы
Ответ 1
Ответ должен быть сделан, чтобы убедиться, что "Целевое членство" в файле sql установлено правильно, чтобы проект "видел" его:
1) щелкните по файлу sql на левой панели Xcode
2) открыть/показать "Инспектор файлов" (правая панель)
3) В разделе "Целевое членство" убедитесь, что "проверка" "отмечена"
что он.
Ответ 2
![enter image description here]()
Это решение разрешает мою проблему, я надеюсь, что это поможет вам.
Ответ 3
в моей проблеме:
проверьте эту строку:
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"users.sql"]
Проверьте имя файла в вашем комплекте и в коде - ошибка также обложена разными именами файлов.