我正在尝试使用此功能更新表中的列:
-(void)updateID:(NSString*)ID{
NSString *queryInsertAndUpdate =
[NSString stringWithFormat:
@"UPDATE mytable set ID = '%@' limit 1",ID];
const char* query=[queryInsertAndUpdate UTF8String];
sqlite3_stmt *stmt = nil;
sqlite3_exec(_database, "BEGIN EXCLUSIVE TRANSACTION", 0, 0, 0);
if (sqlite3_prepare_v2(_database, query, -1, &stmt, NULL) == SQLITE_OK) {
if(sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"Query Executed");
} else {
NSLog(@"Query NOT Executed: %s", sqlite3_errmsg(_database));
}
sqlite3_finalize(stmt);
}else{
NSLog(@"Statement NOT Prepared: %s", sqlite3_errmsg(_database));
}
}
当我运行此函数时,一切正常,并且该列得到更新。我知道是因为我调用了有关特定表的数据库,并检查了结果。事实是,当我调用从另一个类读取表的条目的相同函数时,会得到不同的结果。
具体地说,当我打电话时:
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate initViewController];
在initViewController
内部,我调用了读取表的相同函数,并在更新之前获得了列的条目。
为什么会这样?如果需要,我可以提供更多代码。
答案 0 :(得分:1)
如果您有“ BEGIN独家交易”,则还需要进行COMMIT TRANSACTION