在应用程序中运送预填充的SQLite数据库。启动时DB的路径 [英] Shipping a pre populated SQLite DB within the app. Path of the DB on startup

查看:128
本文介绍了在应用程序中运送预填充的SQLite数据库。启动时DB的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许问题的标题不是很自然的解释,但我不知道如何总结。

Maybe the title of the question is not very self-explanatory but I don't know how to summ it up.

我完成一个应用程序基本上是一个DB。我将在应用程序内运送一个数据库。一切都很好,但现在我来到这个问题。安装应用程序时,需要在设备上创建数据库。所以,我把DB拖到我的Xcode项目中的支持文件文件夹(还是Xcode 4.1,顺便说一下)。我去了AppDelegate.m文件,并寻找(NSPersistentStoreCoordinator *)persistentStoreCoordinator {}方法。

I'm finishing an App which is basically a DB. I will ship a DB within the App. Everything works well but now I came to the problem. When the App is installed, it needs to create the database on the device. So, I dragged the DB to the "Supporting Files" folder in my Xcode project (still Xcode 4.1, by the way). I went to the AppDelegate.m file and looked for the (NSPersistentStoreCoordinator *)persistentStoreCoordinator{} method.

我替换了行:

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

代码:

    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Database.sqlite"];
NSLog(@"%@", storePath);

NSURL *storeURL = [NSURL fileURLWithPath:storePath];

    // Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
    NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Database" ofType:@"sqlite"];
    if (defaultStorePath) {
        [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
    }
}



我从 Ray Wenderlich的教程

事情是编译器给我一个警告。

The thing is that the compiler gives me a warning on the line

NSString * storePath = [[self应用程序目录] stringByAppendingPathComponent:@Database.sqlite];

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Database.sqlite"];

告诉我NSURL可能不响应'stringByAppendingPathComponent',实际上,应用程序崩溃。有趣的是,教程中的例子没有给出任何警告。

that tells me "NSURL may not respond to 'stringByAppendingPathComponent' and, actually, the App crashes because of that. interestingly enough, the example from the tutorial gives no warning whatsoever.

有人可以告诉我我缺少什么?

Somebody could give me a hand in what am I missing?

提前感谢!

推荐答案

您确定[self applicationDocumentsDirectory]会传回NSString吗?

Are you sure [self applicationDocumentsDirectory] returns a NSString? If not, try this

- (NSString *)applicationDocumentsDirectoryString {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

并将[self applicationDocumentsDirectory]更改为[self applicationDocumentsDirectoryString]

And change [self applicationDocumentsDirectory] to [self applicationDocumentsDirectoryString]

这篇关于在应用程序中运送预填充的SQLite数据库。启动时DB的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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