Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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
iPhone上锁定的sqlite文件_Iphone_Sqlite - Fatal编程技术网

iPhone上锁定的sqlite文件

iPhone上锁定的sqlite文件,iphone,sqlite,Iphone,Sqlite,到目前为止,我所有的数据库访问都是读取的,但现在我需要更新(以及在此插入之后) 我在应用程序目录中有一个包含“shows”的数据库(只读),这对我来说没关系(我不想把它复制到documents文件夹,因为它太大了,我不需要更改其中的内容 但我希望用户选择一些节目作为他的最爱。因此,我在documents文件夹中创建了一个数据库,其中有一个表“favorite_shows”。它包含4个字段: ID(原始密钥) 出示身份证 你最喜欢什么 备注(目前尚未使用) 用户可以切换一次“is_favorite

到目前为止,我所有的数据库访问都是读取的,但现在我需要更新(以及在此插入之后)

我在应用程序目录中有一个包含“shows”的数据库(只读),这对我来说没关系(我不想把它复制到documents文件夹,因为它太大了,我不需要更改其中的内容

但我希望用户选择一些节目作为他的最爱。因此,我在documents文件夹中创建了一个数据库,其中有一个表“favorite_shows”。它包含4个字段: ID(原始密钥) 出示身份证 你最喜欢什么 备注(目前尚未使用)

用户可以切换一次“is_favorite”状态,之后我在尝试更新时出错:

SQLITE_BUSY 5/*数据库文件已锁定*/

这是我的代码:

if (sqlite3_open([databasePath UTF8String],&database) == SQLITE_OK){
    sqlStatement = "select * from favorite_shows WHERE (show_id = ?)";
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        sqlite3_bind_int(compiledStatement, 1, self.ID);
        // search for a show
        if(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // if we can find one, toggle the status
            favID       = sqlite3_column_int(compiledStatement, 0); // we need the primary key to update it
            isFav       = (sqlite3_column_int(compiledStatement, 2) == 1); // let's store the favorite status

            sqlStatement = "update favorite_shows SET is_favorite = ? WHERE (ID = ?)";
            if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
                sqlite3_bind_int(compiledStatement, 1, !isFav );                
                sqlite3_bind_int(compiledStatement, 2, favID);
                int error = sqlite3_step(compiledStatement);
                if (SQLITE_DONE != error) {
                    NSLog(@"error while updating favorite status");
                }
            }   
        }
        //else :  no records found indicating that this show hasn't been a favorite yet, so insert one as favorite

sqlite3_finalize(compiledStatement);        
sqlite3_close(database);
}
第二次被锁定的原因是什么?除此之外还有其他指示吗: sqlite3_finalize(编译语句);
sqlite3_关闭(数据库);
是否关闭所有内容?

编辑:

繁忙是由于重新使用
compiledStatement
而没有删除以前编译的语句造成的。您需要使用finalize和close函数正确释放资源。 请参阅此处的文档


编辑:

繁忙是由于重新使用
compiledStatement
而没有删除以前编译的语句造成的。您需要使用finalize和close函数正确释放资源。 请参阅此处的文档


不在单独的线程中,只在主线程中不在单独的线程中,只在主线程中thread@falconcreek那个self.ID是什么?self.ID在原始帖子中。我想ID是在别处定义的类的属性,可以使用Objective-C'dot'访问它syntax@falconcreek那个self.ID是什么?self.ID在原来的帖子里。我假定ID是在别处定义的类的一个属性,该类使用Objective-C“dot”语法访问
const char * select = "select * from favorite_shows WHERE (show_id = ?)";
const char * update = "update favorite_shows SET is_favorite = ? WHERE (ID = ?)";

sqlite3_stmt *selectStmt;
sqlite3_stmt *updateStmt;


if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    if(sqlite3_prepare_v2(database, select, -1, &selectStmt, NULL) == SQLITE_OK) {
        sqlite3_bind_int(selectStmt, 1, self.ID);
        // search for a show
        if(sqlite3_step(selectStmt) == SQLITE_ROW) {
            // if we can find one, toggle the status
            favID       = sqlite3_column_int(selectStmt, 0); // we need the primary key to update it
            isFav       = (sqlite3_column_int(selectStmt, 2) == 1) ? 0 : 1; // Flip is_favorite value
            sqlite3_finalize(selectStmt); // Delete the statement OR create a new one

            if(sqlite3_prepare_v2(database, update, -1, &updateStmt, NULL) == SQLITE_OK) {
                sqlite3_bind_int(updateStmt, 1, isFav );                
                sqlite3_bind_int(updateStmt, 2, favID);
                int error = sqlite3_step(updateStmt);
                if (SQLITE_DONE != error) {
                    NSLog(@"error while updating favorite status");
                } else {
                sqlite3_finalize(updateStmt);
                }
            }
    } //else :  no records found indicating that this show hasn't been a favorite yet, so insert one as favorite
 }
    sqlite3_close(database);
}