• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS sqlite 无法提交,没有事务处于事件状态

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:21:59 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在 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-推荐答案


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_stepsqlite3_finalize

此外,资源是只读的;你必须 copy the database file out of the bundle以便能够对其进行修改。

关于iOS sqlite 无法提交,没有事务处于事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24135596/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap