16
16
src = sqlite3.connect(":memory: ", isolation_level=None)
17
17
dst = sqlite3.connect("tutorial.db", isolation_level=None)
18
18
src.backup(dst)
19
+ src.close()
20
+ dst.close()
19
21
del src, dst
20
22
21
23
.. _sqlite3-intro :
@@ -220,6 +222,7 @@ creating a new cursor, then querying the database:
220
222
>>> title, year = res.fetchone()
221
223
>>> print (f ' The highest scoring Monty Python movie is { title!r } , released in { year} ' )
222
224
The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
225
+ >>> new_con.close()
223
226
224
227
You've now created an SQLite database using the :mod: `!sqlite3 ` module,
225
228
inserted data and retrieved values from it in multiple ways.
@@ -735,6 +738,7 @@ Connection objects
735
738
>>> for row in con.execute(" SELECT md5(?)" , (b " foo" ,)):
736
739
... print (row)
737
740
('acbd18db4cc2f85cedef654fccc4a4d8',)
741
+ >>> con.close()
738
742
739
743
740
744
.. method :: create_aggregate(name, n_arg, aggregate_class)
@@ -871,6 +875,7 @@ Connection objects
871
875
FROM test ORDER BY x
872
876
""")
873
877
print(cur.fetchall())
878
+ con.close()
874
879
875
880
.. testoutput ::
876
881
:hide:
@@ -1161,6 +1166,8 @@ Connection objects
1161
1166
src = sqlite3.connect('example.db')
1162
1167
dst = sqlite3.connect(':memory: ')
1163
1168
src.backup(dst)
1169
+ dst.close()
1170
+ src.close()
1164
1171
1165
1172
.. versionadded :: 3.7
1166
1173
@@ -1227,6 +1234,10 @@ Connection objects
1227
1234
>>> con.getlimit(sqlite3.SQLITE_LIMIT_ATTACHED )
1228
1235
1
1229
1236
1237
+ .. testcleanup :: sqlite3.limits
1238
+
1239
+ con.close()
1240
+
1230
1241
.. versionadded :: 3.11
1231
1242
1232
1243
.. _SQLite limit category : https://www.sqlite.org/c3ref/c_limit_attached.html
@@ -1508,6 +1519,10 @@ Cursor objects
1508
1519
# cur is an sqlite3.Cursor object
1509
1520
cur.executemany("INSERT INTO data VALUES(?)", rows)
1510
1521
1522
+ .. testcleanup :: sqlite3.cursor
1523
+
1524
+ con.close()
1525
+
1511
1526
.. note ::
1512
1527
1513
1528
Any resulting rows are discarded,
@@ -1613,6 +1628,7 @@ Cursor objects
1613
1628
>>> cur = con.cursor()
1614
1629
>>> cur.connection == con
1615
1630
True
1631
+ >>> con.close()
1616
1632
1617
1633
.. attribute :: description
1618
1634
@@ -1733,6 +1749,7 @@ Blob objects
1733
1749
greeting = blob.read()
1734
1750
1735
1751
print(greeting) # outputs "b'Hello, world!'"
1752
+ con.close()
1736
1753
1737
1754
.. testoutput ::
1738
1755
:hide:
@@ -2045,6 +2062,7 @@ Here's an example of both styles:
2045
2062
params = (1972,)
2046
2063
cur.execute("SELECT * FROM lang WHERE first_appeared = ?", params)
2047
2064
print(cur.fetchall())
2065
+ con.close()
2048
2066
2049
2067
.. testoutput ::
2050
2068
:hide:
@@ -2103,6 +2121,7 @@ The object passed to *protocol* will be of type :class:`PrepareProtocol`.
2103
2121
2104
2122
cur.execute("SELECT ?", (Point(4.0, -3.2),))
2105
2123
print(cur.fetchone()[0])
2124
+ con.close()
2106
2125
2107
2126
.. testoutput ::
2108
2127
:hide:
@@ -2133,6 +2152,7 @@ This function can then be registered using :func:`register_adapter`.
2133
2152
2134
2153
cur.execute("SELECT ?", (Point(1.0, 2.5),))
2135
2154
print(cur.fetchone()[0])
2155
+ con.close()
2136
2156
2137
2157
.. testoutput ::
2138
2158
:hide:
@@ -2217,6 +2237,8 @@ The following example illustrates the implicit and explicit approaches:
2217
2237
cur.execute("INSERT INTO test(p) VALUES(?)", (p,))
2218
2238
cur.execute('SELECT p AS "p [point]" FROM test')
2219
2239
print("with column names:", cur.fetchone()[0])
2240
+ cur.close()
2241
+ con.close()
2220
2242
2221
2243
.. testoutput ::
2222
2244
:hide:
@@ -2423,6 +2445,8 @@ Some useful URI tricks include:
2423
2445
res = con2.execute("SELECT data FROM shared")
2424
2446
assert res.fetchone() == (28,)
2425
2447
2448
+ con1.close()
2449
+ con2.close()
2426
2450
2427
2451
More information about this feature, including a list of parameters,
2428
2452
can be found in the `SQLite URI documentation `_.
@@ -2469,6 +2493,7 @@ Queries now return :class:`!Row` objects:
2469
2493
'Earth'
2470
2494
>>> row[" RADIUS" ] # Column names are case-insensitive.
2471
2495
6378
2496
+ >>> con.close()
2472
2497
2473
2498
.. note ::
2474
2499
@@ -2495,6 +2520,7 @@ Using it, queries now return a :class:`!dict` instead of a :class:`!tuple`:
2495
2520
>>> for row in con.execute(" SELECT 1 AS a, 2 AS b" ):
2496
2521
... print (row)
2497
2522
{'a': 1, 'b': 2}
2523
+ >>> con.close()
2498
2524
2499
2525
The following row factory returns a :term: `named tuple `:
2500
2526
@@ -2521,6 +2547,7 @@ The following row factory returns a :term:`named tuple`:
2521
2547
1
2522
2548
>>> row.b # Attribute access.
2523
2549
2
2550
+ >>> con.close()
2524
2551
2525
2552
With some adjustments, the above recipe can be adapted to use a
2526
2553
:class: `~dataclasses.dataclass `, or any other custom class,
0 commit comments