为什么数据库在IOS中以sqlite插入数据时被锁定?

时间:2014-02-06 08:48:23

标签: ios database sqlite

为什么我在sqlite中插入数据时会锁定数据库?我已经打开并关闭了我的所有数据库代码?我有这段代码:

-(void)insertDataIn_tbl_selectItem_data: (NSString *)empID ProdId: (NSString *)prodId ProdName: (NSString *)prodName  GenName: (NSString *)genName ComputeType: (NSString *)computeType UOM: (NSString *)uom ListPrice: (NSString *)listPrice UOMQty: (NSString *)uomqty{

    const char *query = "insert into tbl_selectItem_data (femployeeid,fproductid,fname,fgeneric_name,fcompute_type,fuom,flist_price,fuomqty) values (?,?, ?, ?, ?, ?, ?, ?)";
    sqlite3_stmt *stmt;

    if (sqlite3_open([sqLiteDb UTF8String], &(_database)) == SQLITE_OK) {
        if (sqlite3_prepare_v2(_database, query, -1, &stmt, nil) == SQLITE_OK) {

            sqlite3_bind_text(stmt, 1, [empID UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 2, [prodId UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 3, [prodName UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 4, [genName UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 5, [computeType UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 6, [uom UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 7, [listPrice UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 8, [uomqty UTF8String], -1, SQLITE_TRANSIENT);

            if(sqlite3_step(stmt) == SQLITE_DONE){
                NSLog(@"Insert Successful");
                sqlite3_finalize(stmt);
            }else{
                NSLog(@"insertDataIn_tbl_selectItem_data error: %s", sqlite3_errmsg(_database));
            }
        }
    }

    sqlite3_close(_database);
}

1 个答案:

答案 0 :(得分:1)

首先关闭所有...即使有错误,尝试'敲定'你的陈述......所以你的方法:

if(sqlite3_step(stmt) == SQLITE_DONE){
            NSLog(@"Insert Successful");
            sqlite3_finalize(stmt);
        }else{
            NSLog(@"insertDataIn_tbl_selectItem_data error: %s", sqlite3_errmsg(_database));
        }
    }
}
sqlite3_close(_database);

最好是:

if(sqlite3_step(stmt) == SQLITE_DONE){
            NSLog(@"Insert Successful");
        }else{
            NSLog(@"insertDataIn_tbl_selectItem_data error: %s", sqlite3_errmsg(_database));
        }
    }
 sqlite3_finalize(stmt);
}

sqlite3_close(_database);

第二个......

您可以尝试使用sqlite3_open_v2代替sqlite3_open更多相关内容:http://www.sqlite.org/c3ref/open.html

所以......反而:

if (sqlite3_open([sqLiteDb UTF8String], &(_database)) == SQLITE_OK) {...}

将是:if (sqlite3_open_v2([sqLiteDb UTF8String], &(_database), SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK) {....}