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
BEGINstatement) before data modification statements (INSERT,UPDATE,DELETEorREPLACE); - explicitly ended by the user (with a
COMMITorROLLBACKstatement).
When auto-commit is off, it means that SQL transactions are either:
- implicitly started by the SQL library (with a
BEGINstatement) before data modification statements (INSERT,UPDATE,DELETEorREPLACE); - implicitly ended by the SQL library (with a
COMMITorROLLBACKstatement) after data modification statements (INSERT,UPDATE,DELETEorREPLACE);
or:
- explicitly started by the user (with a
BEGINorSAVEPOINTstatement); - explicitly ended by the user (with a
COMMIT,ROLLBACKorRELEASEstatement, orROLLBACK TO+RELEASEstatements).
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 aBEGINstatement. Autocommit mode is re-enabled by aCOMMITorROLLBACK.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
sqlite3library operates in autocommit mode by default, but the Pythonsqlite3module by default does not.
autocommitmode means that statements that modify the database take effect immediately. ABEGINorSAVEPOINTstatement disablesautocommitmode, and aCOMMIT, aROLLBACK, or aRELEASEthat ends the outermost transaction, turns autocommit mode back on.The Python
sqlite3module by default issues aBEGINstatement implicitly before a Data Modification Language (DML) statement (i.e.INSERT/UPDATE/DELETE/REPLACE).You can control which kind of
BEGINstatementssqlite3implicitly executes via theisolation_levelparameter to theconnect()call, or via theisolation_levelproperty of connections. If you specify no isolation_level, a plainBEGINis used, which is equivalent to specifyingDEFERRED. Other possible values areIMMEDIATEandEXCLUSIVE.You can disable the
sqlite3module’s implicit transaction management by settingisolation_leveltoNone. This will leave the underlyingsqlite3library operating inautocommitmode. You can then completely control the transaction state by explicitly issuingBEGIN,ROLLBACK,SAVEPOINT, andRELEASEstatements in your code.Changed in version 3.6:
sqlite3used 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