1

I use sqlite3 version 2.6.0 python 2.7. Why queries starts with comment ('/* */' or '--') cause an implicit commit?

Example.

#!/usr/bin/python

import os
try:
    os.remove('db1')
except OSError:
    pass

import sqlite3

cur1 = sqlite3.connect('db1').cursor()
cur2 = sqlite3.connect('db1').cursor()

# In first cursor open a transaction
cur1.execute('CREATE TABLE test(t text);')
cur1.execute('BEGIN;')
cur1.execute('INSERT INTO test(t) VALUES (\'123456\');')

# In first cursor first transaction is invisible.
print 'Cursor 1'
print cur1.execute('SELECT * FROM test;').fetchall()

# In second cursor first transaction is invisible.
print 'Cursor 2'
print cur2.execute('SELECT * FROM test;').fetchall()

# In first cursor execute a query, begin with comments.
# Here is implicit commit!
print 'Cursor 1'
print cur1.execute('/* 123 */ SELECT * FROM test;').fetchall()

# In second cursor data now visible!
print 'Cursor 2'
print cur2.execute('SELECT * FROM test;').fetchall()

After a query with comment at the begin, cursor cur1 implicitly commits its changes. Is it a bug or a feature?

0

1 Answer 1

1

This is a long-standing bug in the Python 2 sqlite3 bindings. See issue 10740:

As evidence of that, note that the existing statement detection code is broken: it doesn't strip comments first! A simple SELECT statement preceeded by a comment will implicitly commit!

The bindings will implicitly commit before DDL statements, but the way this is implemented it will also do so when a line starts with a comment.

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

Comments

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.