sqlite3 saving changes without commit command in python

久未见 提交于 2019-12-03 13:54:40

Auto-commit definition

When auto-commit is on, it means that SQL transactions are:

  • implicitly started by the SQL library (with a BEGIN statement) before data modification statements (INSERT, UPDATE, DELETE or REPLACE);
  • explicitly ended by the user (with a COMMIT or ROLLBACK statement).

When auto-commit is off, it means that SQL transactions are either:

  • implicitly started by the SQL library (with a BEGIN statement) before data modification statements (INSERT, UPDATE, DELETE or REPLACE);
  • implicitly ended by the SQL library (with a COMMIT or ROLLBACK statement) after data modification statements (INSERT, UPDATE, DELETE or REPLACE);

or:

  • explicitly started by the user (with a BEGIN or SAVEPOINT statement);
  • explicitly ended by the user (with a COMMIT, ROLLBACK or RELEASE statement, or ROLLBACK TO + RELEASE statements).

Library implementations

The underlying SQLite C library operates by default with auto-commit on:

Test For Auto-Commit Mode

int sqlite3_get_autocommit(sqlite3*);

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.

If certain kinds of errors occur on a statement within a multi-statement transaction (errors including SQLITE_FULL, SQLITE_IOERR, SQLITE_NOMEM, SQLITE_BUSY, and SQLITE_INTERRUPT) then the transaction might be rolled back automatically. The only way to find out whether SQLite automatically rolled back the transaction after an error is to use this function.

If another thread changes the autocommit status of the database connection while this routine is running, then the return value is undefined.

See also lists of Objects, Constants, and Functions.

However, PEP 249 requires that Python database libraries operate by default with auto-commit off:

.commit()

Commit any pending transaction to the database.

Note that if the database supports an auto-commit feature, this must be initially off. An interface method may be provided to turn it back on.

Database modules that do not support transactions should implement this method with void functionality.

Consequently, the SQLite3 Python library operates by default with auto-commit off:

Controlling Transactions

The underlying sqlite3 library operates in autocommit mode by default, but the Python sqlite3 module by default does not.

autocommit mode means that statements that modify the database take effect immediately. A BEGIN or SAVEPOINT statement disables autocommit mode, and a COMMIT, a ROLLBACK, or a RELEASE that ends the outermost transaction, turns autocommit mode back on.

The Python sqlite3 module by default issues a BEGIN statement implicitly before a Data Modification Language (DML) statement (i.e. INSERT/UPDATE/DELETE/REPLACE).

You can control which kind of BEGIN statements sqlite3 implicitly executes via the isolation_level parameter to the connect() call, or via the isolation_level property of connections. If you specify no isolation_level, a plain BEGIN is used, which is equivalent to specifying DEFERRED. Other possible values are IMMEDIATE and EXCLUSIVE.

You can disable the sqlite3 module’s implicit transaction management by setting isolation_level to None. This will leave the underlying sqlite3 library operating in autocommit mode. You can then completely control the transaction state by explicitly issuing BEGIN, ROLLBACK, SAVEPOINT, and RELEASE statements in your code.

Changed in version 3.6: sqlite3 used to implicitly commit an open transaction before DDL statements. This is no longer the case.

The behaviour of the SQLite3 Python library can be checked with this Python program:

import sqlite3

# Auto-commit is off (default).

connection = sqlite3.connect("test.sqlite")
cursor = connection.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS t (i INTEGER)")
cursor.execute("INSERT INTO t VALUES (?)", (5,))
cursor.close()
connection.close()

connection = sqlite3.connect("test.sqlite")
cursor = connection.cursor()
cursor.execute("SELECT * FROM t")
assert cursor.fetchall() == []  # data had not been committed
cursor.close()
connection.close()

# Auto-commit is on.

connection = sqlite3.connect("test.sqlite", isolation_level=None)
cursor = connection.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS t (i INTEGER)")
cursor.execute("INSERT INTO t VALUES (?)", (5,))
cursor.close()
connection.close()

connection = sqlite3.connect("test.sqlite", isolation_level=None)
cursor = connection.cursor()
cursor.execute("SELECT * FROM t")
assert cursor.fetchall() == [(5,)]  # data had been committed
cursor.close()
connection.close()

Note. — The second assertion would fail if an in-memory database was used instead of an on-disk database (by passing the ":memory:" argument instead of "test.sqlite" to the sqlite3.connect function), since in-memory databases are dropped when the connection is closed.

This information will hopefully help you answer to your question.

Probably autocommit is on, it is by default http://www.sqlite.org/c3ref/get_autocommit.html

Python sqlite3 issues a BEGIN statement automatically before "INSERT" or "UPDATE". After that it automatically commits on any other command or db.close()

Add isolation_level=None to connect (Ref)

db = sqlite.connect(":memory:", isolation_level=None)

also connection objects can be used as context managers that automatically commit or rollback transactions. 11.13.7.3. on docs.python

# Successful, con.commit() is called automatically afterwards
with con:
    con.execute("insert into person(firstname) values (?)", ("Joe",))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!