当我在模拟器上测试我的应用程序时,一切正常。但是,当我在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");
}
最佳答案
在打开数据库时,您可以将其与else案例一起调用以解决问题。
if (sqlite3_open([getDBPath UTF8String], &sqlhandle) == SQLITE_OK)
{
NSLog(@"Database Open!");
}
else
{
NSLog(@"Select err: %s",sqlite3_errmsg(sqlhandle));
}
您将能够使用此命令获得正确的错误消息
关于iphone - SQLite数据库未在iPhone设备上加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24562761/