Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法访问iOS SQLite数据库:sqlite3_prepare_v2和';没有此类表格';sqlite3_errmsg_Ios_Ios5_Sqlite_Ios Simulator - Fatal编程技术网

无法访问iOS SQLite数据库:sqlite3_prepare_v2和';没有此类表格';sqlite3_errmsg

无法访问iOS SQLite数据库:sqlite3_prepare_v2和';没有此类表格';sqlite3_errmsg,ios,ios5,sqlite,ios-simulator,Ios,Ios5,Sqlite,Ios Simulator,这是我第一次尝试在iOS中创建和访问SQLite db,我遵循了一个教程,但我无法做到这一点。以下是我遵循的步骤: 通过Firefox插件(“testDatabase”)和 包含多个列的表(“testTable”) 将“testDatabase.sqlite”文件保存在一个目录中 将此类文件拖放到我的“支持文件”组中 Xcode项目 在构建阶段>链接二进制文件中添加了“libsqlite3.dylib” 图书馆 在生成设置>链接>其他链接器标志> 调试和发布 在ViewController中,称

这是我第一次尝试在iOS中创建和访问SQLite db,我遵循了一个教程,但我无法做到这一点。以下是我遵循的步骤:

  • 通过Firefox插件(“testDatabase”)和 包含多个列的表(“testTable”)
  • 将“testDatabase.sqlite”文件保存在一个目录中
  • 将此类文件拖放到我的“支持文件”组中 Xcode项目
  • 在构建阶段>链接二进制文件中添加了“libsqlite3.dylib” 图书馆
  • 在生成设置>链接>其他链接器标志> 调试和发布
  • 在ViewController中,称为此方法:

    -(NSString *)copyDatabaseToDocuments {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0];
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"testDatabase.sqlite"];
        if ( ![fileManager fileExistsAtPath:filePath] ) {
        NSString *bundlePath = [[[NSBundle mainBundle] resourcePath]
                                stringByAppendingPathComponent:@"testDatabase.sqlite"];
            [fileManager copyItemAtPath:bundlePath toPath:filePath error:nil];
        }
        return filePath;
    }
    
  • 然后,尝试在数据库中写入:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"testDatabase.sqlite"];
    sqlite3 *database;
    
    if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
        const char *sqlStatement = "insert into testTable (id) VALUES (?)";
        sqlite3_stmt *compiledStatement;
    
        if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
    
            sqlite3_bind_int(compiledStatement, 1, newObject.ID);
        }
    
        if(sqlite3_step(compiledStatement) == SQLITE_DONE) {
                sqlite3_finalize(compiledStatement);
        }
    }
    sqlite3_close(database);
    
  • 我读过几篇关于类似错误的帖子,但我所尝试的没有一个对我有用。。。我还尝试了使用
    NSString*filePath=[[NSBundle mainBundle]pathForResource:@“testDatabase”类型:@“sqlite”]
    但是它是空的,并且多次在模拟器中低估并重新部署了我的应用程序


    提前感谢

    我通过简单地删除在/Users/[user]/Library/Application Support/iPhone Simulator/5.1/Applications/中生成的应用程序文件夹,最终实现了这一点。当运行模拟器时,将再次创建该文件夹

    当您构建应用程序时,您是否验证了testDatabase.sqlite文件实际上是应用程序包的一部分(与Info.plist和您的其他资源位于同一目录中?是的,它位于该目录中,并且也在目标的构建阶段>复制捆绑资源中列出。)