How to disable autocommit in sqlite4java?

后端 未结 3 816
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 11:48

I have recently been playing with the sqlite4java library. 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

相关标签:
3条回答
  • 2021-01-13 12:12

    Referring to 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.

    So you disable auto-commit by a BEGIN TRANSACTION statement. No separate API function is present for this

    0 讨论(0)
  • 2021-01-13 12:16

    Edit: I confused sqlite4java with the sqliteJDBC package. So the below code will not help. I'm keeping it for reference nevertheless.

    After you obtained the connection, just call setAutoCommit(false)

    java.sql.Connection con = DriverManager.getConnection("jdbc:sqlite:/your/db/file", "user", "password");
    con.setAutoCommit(false);
    
    0 讨论(0)
  • 2021-01-13 12:34

    Jefromi and king_nak are correct - you just need to issue SQL statements that begin and end a transaction.

    SQLiteConnection con = new SQLiteConnection();
    con.exec("BEGIN");
    // do transaction work - auto-commit is disabled
    con.exec("COMMIT");
    // auto-commit is enabled again
    
    0 讨论(0)
提交回复
热议问题