sydent icon indicating copy to clipboard operation
sydent copied to clipboard

Use SQLite context manager for queries

Open anoadragon453 opened this issue 4 years ago • 0 comments

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.

anoadragon453 avatar Jan 28 '20 12:01 anoadragon453