尝试创建数据库时应用程序崩溃

编程入门 行业动态 更新时间:2024-10-24 04:33:01
本文介绍了尝试创建数据库时应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用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.

更多推荐

尝试创建数据库时应用程序崩溃

本文发布于:2023-10-28 19:39:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时应   数据库   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!