简体   繁体   English

如何在sqlite4java中禁用自动提交?

[英]How to disable autocommit in sqlite4java?

I have recently been playing with the sqlite4java library. 我最近一直在玩sqlite4java库。 I think I have it pretty much figured out. 我想我已经弄明白了。 The only think that bothers me is that I do not know how to switch off the auto-commit using this library. 困扰我的唯一想法是我不知道如何使用这个库关闭自动提交。 Can anyone please help? 有人可以帮忙吗? A code example would be much appreciated. 一个代码示例将非常感激。

Thanks in advance, Boro 先谢谢,博罗

Jefromi and king_nak are correct - you just need to issue SQL statements that begin and end a transaction. Jefromi和king_nak是正确的 - 您只需要发出开始和结束事务的SQL语句。

SQLiteConnection con = new SQLiteConnection();
con.exec("BEGIN");
// do transaction work - auto-commit is disabled
con.exec("COMMIT");
// auto-commit is enabled again

Edit : I confused sqlite4java with the sqliteJDBC package. 编辑 :我把sqlite4java与sqliteJDBC包混淆了。 So the below code will not help. 所以下面的代码无济于事。 I'm keeping it for reference nevertheless. 不过我还是要保留它作为参考。

After you obtained the connection, just call setAutoCommit(false) 获得连接后,只需调用setAutoCommit(false)

java.sql.Connection con = DriverManager.getConnection("jdbc:sqlite:/your/db/file", "user", "password");
con.setAutoCommit(false);

Referring to SQLite's C interface description : 参考SQLite的C接口描述

The sqlite3_get_autocommit() interface returns non-zero or zero if the given database connection is or is not in autocommit mode, respectively. 如果给定的数据库连接分别处于或不处于自动提交模式,则sqlite3_get_autocommit()接口将返回非零或零。 Autocommit mode is on by default. 默认情况下,自动提交模式处于启用状态。 Autocommit mode is disabled by a BEGIN statement. BEGIN语句禁用自动提交模式。 Autocommit mode is re-enabled by a COMMIT or ROLLBACK. 通过COMMIT或ROLLBACK重新启用自动提交模式。

So you disable auto-commit by a BEGIN TRANSACTION statement. 因此,您可以通过BEGIN TRANSACTION语句禁用自动提交。 No separate API function is present for this 没有单独的API函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM