iphone - SQLite数据库未在iPhone设备上加载

标签 iphone xcode sqlite ios-simulator

当我在模拟器上测试我的应用程序时,一切正常。但是,当我在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/

相关文章:

objective-c - 如何在 xcode 中自动打印异常?

sqlite - Mono + SQLite + Dapper SQLite 插入期间出现异常

c++ - 使用 CppSqlite3U 中的 sqlite3.exec()

iphone - AFJSONRequestOperation因“数据参数为零”而崩溃NSJSONSerialization错误

iphone - iOS:使用没有文件扩展名的 [UIImage -imageNamed:] 会导致性能问题吗?

ios - 如何以编程方式循环更新 UITextField 中的文本

iphone - 数学(几何): Rectangle projection?

python - 在 Xcode 下运行 virtualenv python

objective-c - 如何检查SQLite数据库是否被锁定

Xcode 重复设备错误