【发布时间】:2014-08-25 02:41:29
【问题描述】:
当我在模拟器上测试我的应用程序时,一切正常。但是当我在 iPhone 上测试应用程序时,它不会从 db.sqlite3 数据库加载。
我尝试了互联网上的所有解决方案。我清理了应用程序。我将数据库添加到构建短语中 - 复制捆绑资源等。 你能帮助我吗?我的编码有任何失败吗?
-(void)openDataBase{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
dbPath = [documentsDirectory stringByAppendingString:@"db.sqlite3"];
//load it
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *pathInRessource = [[NSBundle mainBundle] pathForResource:@"db" ofType:@"sqlite3"];
//exist it?
if (![fileManager fileExistsAtPath:dbPath]) {
[fileManager copyItemAtPath:pathInRessource toPath:dbPath error:nil];
}
//open database
int result = sqlite3_open([dbPath UTF8String], &db);
if (result != SQLITE_OK) {
sqlite3_close(db);
return;
}
//if ok
NSLog(@"Data base opened");
}
【问题讨论】:
-
好吧,你有一堆代码。当你走过它时,你什么时候注意到有什么不对劲的地方?您忽略了哪些错误消息?
-
嘿,在模拟器上我收到消息“数据库已打开”,但是当我在 iphone 上运行它时,我没有收到任何消息。我想,iphone找不到路径。但我不知道如何解决。
-
我会专门查看
sqlite3_open()返回的内容,并对照sqlite.org/c3ref/c_abort.html 进行检查 -
我的猜测是你得到了错误的路径(这可以解释为什么它在模拟器中工作,它是不同的),它应该看起来像
file:///var/mobile/Applications/...id.../Documents/YourDatabase.sqlite。尝试复制代码以从其他来源查找路径。 -
According to the docs 它返回一个
BOOL,而不是一个字符串。您可能也不应该像使用nil那样吞下错误
标签: iphone xcode sqlite ios-simulator