我創建了一個SQL數據庫使用「SQLite數據庫瀏覽器」,拖放到我的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;
}
同樣,這部作品在模擬器上,而不是在iPhone上。 (這不可能與文件有一個「.sql」擴展名而不是「.sqlite」擴展名有關,可以嗎?因爲這是「SQLite數據庫瀏覽器」給它創建的文件的擴展名。 。)
據我所知,Cocoa錯誤260是NSFileReadNoSuchFileError,所以可能是因爲擴展。 – Vin
好吧,我只是改變了文件名以「.sqlite」結尾 - 並將文件重新導入Xcode,並更改爲代碼以匹配新文件名 - 它仍然崩潰 - 同樣的原因。所以不是這樣...... – sirab333
嘗試運行你在設備上使用的教程的代碼,看看是否有效。如果是這樣,db文件本身可能有問題。 – Vin