i read somewhere to save data to a sqlite3 database in python you got to call the commit() function on the connection object. i never do this but my database still gets the data saved to it... why?
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
orREPLACE
); - explicitly ended by the user (with a
COMMIT
orROLLBACK
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
orREPLACE
); - implicitly ended by the SQL library (with a
COMMIT
orROLLBACK
statement) after data modification statements (INSERT
,UPDATE
,DELETE
orREPLACE
);
or:
- explicitly started by the user (with a
BEGIN
orSAVEPOINT
statement); - explicitly ended by the user (with a
COMMIT
,ROLLBACK
orRELEASE
statement, orROLLBACK 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 aBEGIN
statement. Autocommit mode is re-enabled by aCOMMIT
orROLLBACK
.If certain kinds of errors occur on a statement within a multi-statement transaction (errors including
SQLITE_FULL
,SQLITE_IOERR
,SQLITE_NOMEM
,SQLITE_BUSY
, andSQLITE_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 Pythonsqlite3
module by default does not.
autocommit
mode means that statements that modify the database take effect immediately. ABEGIN
orSAVEPOINT
statement disablesautocommit
mode, and aCOMMIT
, aROLLBACK
, or aRELEASE
that ends the outermost transaction, turns autocommit mode back on.The Python
sqlite3
module by default issues aBEGIN
statement implicitly before a Data Modification Language (DML) statement (i.e.INSERT
/UPDATE
/DELETE
/REPLACE
).You can control which kind of
BEGIN
statementssqlite3
implicitly executes via theisolation_level
parameter to theconnect()
call, or via theisolation_level
property of connections. If you specify no isolation_level, a plainBEGIN
is used, which is equivalent to specifyingDEFERRED
. Other possible values areIMMEDIATE
andEXCLUSIVE
.You can disable the
sqlite3
module’s implicit transaction management by settingisolation_level
toNone
. This will leave the underlyingsqlite3
library operating inautocommit
mode. You can then completely control the transaction state by explicitly issuingBEGIN
,ROLLBACK
,SAVEPOINT
, andRELEASE
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",))
来源:https://stackoverflow.com/questions/4699605/sqlite3-saving-changes-without-commit-command-in-python