blob: 7af4ad1ecfbccaffc62df77b5726a3e268fa566b [file] [log] [blame]
Gerhard Häring0d7d6cf2008-03-29 01:32:44 +00001import sqlite3
2
3con = sqlite3.connect(":memory:")
4con.execute("create table person (id integer primary key, firstname varchar unique)")
5
6# Successful, con.commit() is called automatically afterwards
7with con:
8 con.execute("insert into person(firstname) values (?)", ("Joe",))
9
10# con.rollback() is called after the with block finishes with an exception, the
Sandro Tosi567493f2011-10-31 02:41:06 +010011# exception is still raised and must be caught
Gerhard Häring0d7d6cf2008-03-29 01:32:44 +000012try:
13 with con:
14 con.execute("insert into person(firstname) values (?)", ("Joe",))
15except sqlite3.IntegrityError:
16 print("couldn't add Joe twice")