Skip to content

Instantly share code, notes, and snippets.

@Mytherin
Created October 17, 2019 22:32
Show Gist options
  • Save Mytherin/25c79b75446314659df42995c43158e8 to your computer and use it in GitHub Desktop.
Save Mytherin/25c79b75446314659df42995c43158e8 to your computer and use it in GitHub Desktop.
import sqlite3
import os
import time
# showcase of a bug in the python version of sqlite3
# starting an insert with a comment will result in much slower performance and rowcount not being set properly
def test(with_comment):
os.system('rm -f test.db')
con = sqlite3.connect('test.db')
c = con.cursor()
start = time.time()
c.execute('CREATE TABLE integers(i INTEGER)')
if with_comment:
insert_query = """
-- insert many values into the db
INSERT INTO integers VALUES ($1)
"""
else:
insert_query = 'INSERT INTO integers VALUES ($1)'
c.executemany(insert_query, [(x,) for x in range(0, 10000)])
print(c.rowcount)
con.commit()
end = time.time()
return str(end - start)
# around ~0.01 seconds
print('normal executemany time: ' + test(False) + "s")
# around ~10 seconds
print('comment executemany time: ' + test(True) + "s")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment