I am stumped in the behavior of python's builtin module sqlite3. The following code prints out the data inserted regardless of if I comment out/in the line containing commit statement. How can that be?
My understanding from reading the python doc is that while sqlite3's underlying C library has autocommit enabled by default, the python binding does not. Instead, I have to add isolation_level=None to connect() call in order to enable auto-commit. I am asking this because I want to turn off auto-commit and could not find a way to do so.
My Python version is 3.9.2, and since sqlite3 is a builtin module, the module version is also 3.9.2 I think.
import sqlite3
import os
if os.path.exists("dummy.db"):
os.system('rm dummy.db')
assert not os.path.exists("dummy.db") #ensure dummy.db is a new db every run
# Write
c = sqlite3.connect("dummy.db")
ddl = '''--sql
CREATE TABLE foo_table (foo INTEGER, bar INTEGER, foobar INTEGER);
'''
c.execute(ddl)
ddl = '''--sql
INSERT INTO foo_table VALUES(1,2,3);
'''
c.execute(ddl)
#c.commit()
c.close()
# Read
c = sqlite3.connect("dummy.db")
ddl = 'SELECT * FROM foo_table'
for row in c.execute(ddl):
print(row)
Output
>>(1, 2, 3)