iOS sqlite 无法提交,没有事务处于事件状态
<p><p>我在 iOS 上做一个按钮来更新 SQlite 数据。我可以使用 Firefox SQLite Manager 更新值,但是使用下面的代码会出现“无法提交,没有事务处于事件状态”的问题。请阐明一些观点。谢谢。</p>
<p>=
- (id *)updateBanks{</p>
<pre><code>sqlite3_stmt *statement = NULL;
const char *branch_no = "MAYBANK1";
const char *address = "KLCC";
NSFileManager *fileMgr = ;
NSString *dbPath = [[ resourcePath ]stringByAppendingPathComponent:@"banks.sqlite"];
BOOL success = ;
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open(, &db) == SQLITE_OK))
{
NSLog(@"An error has occured.");
}
if(sqlite3_open(, &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, , -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;
</code></pre>
<p>}</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><a href="http://www.sqlite.org/lang_transaction.html" rel="noreferrer noopener nofollow">documentation</a>说:</p>
<blockquote>
<p>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.</p>
<p>Transactions can be started manually using the BEGIN command. Such transactions usually persist until the next COMMIT or ROLLBACK command.</p>
</blockquote>
<p>所以你的 UPDATE 语句的自动事务已经被提交了。
仅当您还运行过 BEGIN 时才需要运行 COMMIT。</p>
<hr/>
<p>另外,你不能调用 <code>sqlite3_open</code> 两次;第一个数据库连接将被泄露。</p>
<p>另外,您必须在执行 COMMIT 之前调用 <code>sqlite3_step</code> 和 <code>sqlite3_finalize</code> <em></em>。</p>
<p>此外,资源是只读的;你必须 <a href="https://stackoverflow.com/questions/4743317/iphone-how-to-copy-a-file-from-resources-to-documents" rel="noreferrer noopener nofollow">copy the database file out of the bundle</a>以便能够对其进行修改。</p></p>
<p style="font-size: 20px;">关于iOS sqlite 无法提交,没有事务处于事件状态,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24135596/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24135596/
</a>
</p>
页:
[1]