Mercurial > cpython-lru_cache-weakref
annotate Doc/includes/sqlite3/ctx_manager.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | bc16d1300ee1 |
| children |
| rev | line source |
|---|---|
|
46181
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
1 import sqlite3 |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
2 |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
3 con = sqlite3.connect(":memory:") |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
4 con.execute("create table person (id integer primary key, firstname varchar unique)") |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
5 |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
6 # Successful, con.commit() is called automatically afterwards |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
7 with con: |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
8 con.execute("insert into person(firstname) values (?)", ("Joe",)) |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
9 |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
10 # con.rollback() is called after the with block finishes with an exception, the |
|
73230
bc16d1300ee1
caught is the right pp of catch; thanks to Don Bennett from docs@
Sandro Tosi <sandro.tosi@gmail.com>
parents:
46181
diff
changeset
|
11 # exception is still raised and must be caught |
|
46181
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
12 try: |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
13 with con: |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
14 con.execute("insert into person(firstname) values (?)", ("Joe",)) |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
15 except sqlite3.IntegrityError: |
|
09053a7c1cdf
Same documentation for sqlite3 module as in 2.6.
Gerhard Häring <gh@ghaering.de>
parents:
diff
changeset
|
16 print("couldn't add Joe twice") |
