Staging
v0.8.1
https://github.com/python/cpython
Revision 426b022776672fdf3d71ddd98d89af341c88080f authored by Larry Hastings on 05 September 2020, 07:22:07 UTC, committed by Larry Hastings on 05 September 2020, 07:22:07 UTC
1 parent f2f17bd
Raw File
Tip revision: 426b022776672fdf3d71ddd98d89af341c88080f authored by Larry Hastings on 05 September 2020, 07:22:07 UTC
Version bump for 3.5.10.
Tip revision: 426b022
ctx_manager.py
import sqlite3

con = sqlite3.connect(":memory:")
con.execute("create table person (id integer primary key, firstname varchar unique)")

# Successful, con.commit() is called automatically afterwards
with con:
    con.execute("insert into person(firstname) values (?)", ("Joe",))

# con.rollback() is called after the with block finishes with an exception, the
# exception is still raised and must be caught
try:
    with con:
        con.execute("insert into person(firstname) values (?)", ("Joe",))
except sqlite3.IntegrityError:
    print("couldn't add Joe twice")
back to top