You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

16 lines
568 B

  1. import sqlite3
  2. con = sqlite3.connect(":memory:")
  3. con.execute("create table person (id integer primary key, firstname varchar unique)")
  4. # Successful, con.commit() is called automatically afterwards
  5. with con:
  6. con.execute("insert into person(firstname) values (?)", ("Joe",))
  7. # con.rollback() is called after the with block finishes with an exception, the
  8. # exception is still raised and must be catched
  9. try:
  10. with con:
  11. con.execute("insert into person(firstname) values (?)", ("Joe",))
  12. except sqlite3.IntegrityError:
  13. print("couldn't add Joe twice")