有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我最近一直在玩sqlite4java库。 我想我已经大致弄明白了。唯一困扰我的是我不知道如何使用这个库关闭自动提交。有人能帮忙吗? 如能提供代码示例,将不胜感激

提前感谢,, 波罗


共 (3) 个答案

  1. # 1 楼答案

    编辑:我混淆了sqlite4java和sqliteJDBC包。所以下面的代码没有帮助。尽管如此,我还是保留它作为参考

    获得连接后,只需调用setAutoCommit(false)

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

    关于SQLite's C interface description

    The sqlite3_get_autocommit() interface returns non-zero or zero if the given database connection is or is not in autocommit mode, respectively. Autocommit mode is on by default. Autocommit mode is disabled by a BEGIN statement. Autocommit mode is re-enabled by a COMMIT or ROLLBACK.

    因此,可以通过BEGIN TRANSACTION语句禁用自动提交。没有为此提供单独的API函数

  3. # 3 楼答案

    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