2

I have made it a habbit that when i do cur.execute() functions, I do a sql.commit() function right after.

Is sql.commit() needed if I do not modify the database (i.e. only do a SELECT query)?

Here is an example:

sql = sqlite3.connect('DB.db')
cur = sql.cursor()
cur.execute('SELECT * FROM table')
sql.commit()
4
  • If it a simple select statement like in your example, there is no reason to perform a commit. Commented Nov 16, 2015 at 1:46
  • Actually, this might help provide more context. Let me know if this was helpful: stackoverflow.com/questions/14430424/commit-after-select Commented Nov 16, 2015 at 1:47
  • My select queries are simple and do not modify the database. They just pull info. Commented Nov 16, 2015 at 1:53
  • The example in the docs implies you can use execute: c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol) docs.python.org/2/library/sqlite3.html Commented Nov 16, 2015 at 1:57

1 Answer 1

6

Python does not begin a transaction for select statements.

From the Transaction control section of the sqlite3 module docs:

By default, the sqlite3 module opens transactions implicitly before a Data Modification Language (DML) statement (i.e. INSERT/UPDATE/DELETE/REPLACE), and commits transactions implicitly before a non-DML, non-query statement (i. e. anything other than SELECT or the aforementioned).

Sign up to request clarification or add additional context in comments.

1 Comment

(since english is not my mother tongue, I'm confused by the latter part of this doc) does and commits transactions implicitly before a non-DML, non-query statement means sqlite3 will implicitly do commits for both non-DML (insert/delete/update/create) and non-query (anything other than select) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.