sydent
sydent copied to clipboard
Use SQLite context manager for queries
https://docs.python.org/2/library/sqlite3.html#using-the-connection-as-a-context-manager
This allows us to wrap queries in a block:
try:
with con:
con.execute("insert into person(firstname) values (?)", ("Joe",))
except sqlite3.IntegrityError:
print "couldn't add Joe twice"
that will automatically commit upon success (though note that there are some places where we explicitly do not commit the DB) and roll back upon error.