代码之家  ›  专栏  ›  技术社区  ›  Glenn

iPhone上锁定的sqlite文件

  •  1
  • Glenn  · 技术社区  · 14 年前

    到目前为止,我所有的数据库访问权限都在读取,但现在我需要更新(在这个插入之后)

    我在应用程序目录中有一个包含“显示”的数据库(只读),这对我来说没问题(我不想把它复制到文档文件夹中,因为它很大,我不需要更改其中的内容。

    但我希望用户选择一些节目作为自己的最爱。因此,我在documents文件夹中创建了一个带有“favorite_show”表的数据库。它包含4个字段: ID(主键) 显示ID 是你的最爱 备注(目前尚未使用)

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

    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(compiledStatement);
    sqlite3_close(数据库); 关闭一切?

    1 回复  |  直到 14 年前
        1
  •  8
  •   falconcreek    14 年前

    编辑:

    忙碌是重新使用的结果 compiledStatement 不删除以前编译的语句。您需要使用Finalize和Close函数适当地释放资源。 请参阅此处的文档。 http://sqlite.org/c3ref/stmt.html

    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);
    }
    
    推荐文章