我在 iOS 上做一个按钮来更新 SQlite 数据。我可以使用 Firefox SQLite Manager 更新值,但是使用下面的代码会出现“无法提交,没有事务处于事件状态”的问题。请阐明一些观点。谢谢。
=
- (id *)updateBanks{
sqlite3_stmt *statement = NULL;
const char *branch_no = "MAYBANK1";
const char *address = "KLCC";
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent"banks.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured.");
}
if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)
{
const char *sql = "Update Maybank Set address = ? Where branch_no = ?";
if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL)==SQLITE_OK){
sqlite3_bind_text(statement, 1, address, -1, SQLITE_TRANSIENT);
//sqlite3_bind_text(updateStmt, 1, [address.text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, branch_no, -1, SQLITE_TRANSIENT);
}
}
char* errmsg;
sqlite3_exec(db, "COMMIT", NULL, NULL, &errmsg);
if(SQLITE_DONE != sqlite3_step(statement)){
NSLog(@"Error while updating. %s", sqlite3_errmsg(db));
}
else{
}
sqlite3_finalize(statement);
sqlite3_close(db);
return 0;
}
Best Answer-推荐答案 strong>
documentation说:
Any command that changes the database (basically, any SQL command other than SELECT) will automatically start a transaction if one is not already in effect. Automatically started transactions are committed when the last query finishes.
Transactions can be started manually using the BEGIN command. Such transactions usually persist until the next COMMIT or ROLLBACK command.
所以你的 UPDATE 语句的自动事务已经被提交了。
仅当您还运行过 BEGIN 时才需要运行 COMMIT。
另外,你不能调用 sqlite3_open 两次;第一个数据库连接将被泄露。
另外,您必须在执行 COMMIT 之前调用 sqlite3_step 和 sqlite3_finalize 。
此外,资源是只读的;你必须 copy the database file out of the bundle以便能够对其进行修改。
关于iOS sqlite 无法提交,没有事务处于事件状态,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/24135596/
|