尝试创建数据库时应用程序崩溃 [英] App crashes on Database Creation attempt

查看:139
本文介绍了尝试创建数据库时应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SQLite数据库浏览器创建了一个sql数据库,将其拖放到我的Xcode项目中,并构建了该应用程序。它在模拟器上运行得很好,但在iPhone上崩溃,出现以下错误:

  ***由于未捕获异常终止应用程序'NSInternalInconsistencyException',
reason:'无法创建带有消息的可写数据库文件'操作可能,
未完成。 (Cocoa error 260.)'。'

这是我的代码:

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
//创建可写副本在应用程序文档目录中绑定的默认数据库:
NSLog(@AppDelegate ...寻找嵌入式数据库文件...);
BOOL成功;
NSFileManager * fileManager = [NSFileManager defaultManager];
NSError * error;
//获取文档文件夹的路径:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * writableDBPath = [documentsDirectory stringByAppendingPathComponent:@users.sql];

success = [fileManager fileExistsAtPath:writableDBPath];
if(success){
NSLog(@数据库文件存在于文档文件夹!
NSLog(@它的路径是:%@,writableDBPath);
return YES;
}
else {
//但如果可写数据库不存在,请将默认值复制到适当的位置。
NSLog(@!!数据库文件存在于文档文件夹!);
NSString * defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@users.sql];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:& error];
if(!success){
NSAssert1(0,@无法创建带有消息'%@'。的可写数据库文件',[error localizedDescription]);
}
else
NSLog(@WROTE THE DATABASE FILE !!!);
}

return YES;
}

同样,这在模拟器上工作,但不是在iPhone上。 (这不可能与文件有一个.sql扩展而不是一个.sqlite扩展有什么关系吗?因为这是SQLite数据库浏览器给它创建的文件的扩展名。 。)

解决方案

答案与确保sql文件的目标成员资格设置正确,项目看到它:



1)单击Xco​​de左窗格中的sql文件



2)打开/显示文件检查器(右窗格)



3)在目标成员下,确保检查

就是这样。


I created an sql database using "SQLite Database Browser", dragged and dropped it into my Xcode project, and built the app. It works perfectly well on the Simulator but crashes on the iPhone, with this error:

*** 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.)'.'   

Here's my code:

- (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;
}

Again, this works on the Simulator, but not on the iPhone. (This couldn't possible have anything to do with the file have a ".sql" extension as opposed to a ".sqlite" extension, could it? Cause that's the extensions that "SQLite Database Browser" gives the files it creates...)

解决方案

The answer has to do with making sure the "Target Membership" of the sql file is set properly, so that the project "sees" it:

1) click on the sql file in the left-pane of Xcode

2) open/show the File Inspector (right pane)

3) Under "Target Membership", make sure the "check" is "checked"

that's it.

这篇关于尝试创建数据库时应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆