使用app和stringByAppendingPathComponent错误捆绑sqlite数据库 [英] bundle sqlite database with app and stringByAppendingPathComponent error

查看:175
本文介绍了使用app和stringByAppendingPathComponent错误捆绑sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能还有很多其他问题,因为我是新的应用程序编程。



我最初创建的数据库从应用程序,将其复制到我的工作文件夹(这可能不是最终应驻留的位置),然后从文本文件中附加我的记录(大约1,000个)。



前面提到的两个问题是:
- 数据库应该在什么文件夹中?
- 它是如何与应用程序一起部署的?



我发现很多例子使用persistStoreCoordinator函数中的以下行:

  NSString * storePath = [[self applicationDocumentsDirectory] ​​stringByAppendingPathComponent:@myDatabase.sqlite]; 
NSURL * storeUrl = [NSURL fileURLWithPath:storePath];

但是第一行给我的预编译错误:Receiver type'NSURL'for instance message没有声明一个方法选择器'stringByAppendingPathComponent:',为什么它不为我工作?



这是事实上最好的方式绑定我的数据库与休息

解决方案

最简单的解决方案是使用NSUrl而不是NSString。SO user @trapper已经在下面的链接中提供了一个解决方案。



将sqlite导入到coredata

  NSURL * storeURL = [[self applicationDocumentsDirectory ] URLByAppendingPathComponent:@Database.sqlite]; 

//如果数据库不存在在默认的一个
if(![storeURL checkResourceIsReachableAndReturnError:NULL])复制
{
NSURL * defaultStoreURL = [[NSBundle mainBundle] URLForResource:@DatabasewithExtension:@sqlite];

if([defaultStoreURL checkResourceIsReachableAndReturnError:NULL])
{
NSFileManager * fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
}
}


There are probably many other questions I don't even know to ask yet since I'm new to app programming.

I initially created the database from within the app, copied it to my working folder (which is probably not where it should ultimately reside), then appended my records (about 1,000 of them) from a text file.

The first two questions that come to mind are: - what folder should the database be in? - how does it get deployed with the app?

I found quite few examples using the following lines in persistentStoreCoordinator function:

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"myDatabase.sqlite"];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

But the first line gives me the pre-compile error: "Receiver type 'NSURL' for instance message does not declare a method with selector 'stringByAppendingPathComponent:'. Why is it not working for me?

And is this in fact the best way to bundle my database with the rest of the app?

Thanks!

解决方案

Easiest solution is to use NSUrl instead of NSString. SO user @trapper already provided a solution in the below link.

importing sqlite to coredata

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Database.sqlite"];

// If the database doesn't exist copy in the default one
if (![storeURL checkResourceIsReachableAndReturnError:NULL])
{
    NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"Database" withExtension:@"sqlite"];

    if ([defaultStoreURL checkResourceIsReachableAndReturnError:NULL])
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
    }
}

这篇关于使用app和stringByAppendingPathComponent错误捆绑sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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