mirror of
https://github.com/python/cpython.git
synced 2025-07-23 11:15:24 +00:00
bpo-34580: Update sqlite3 examples to call close() explicitly (GH-9079)
The sqlit3.Connection object doesn't call its close() method when it's used as a context manager.
This commit is contained in:
parent
5c08ce9bf7
commit
287b84de93
26 changed files with 52 additions and 19 deletions
|
@ -13,3 +13,5 @@ cur = con.cursor()
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
cur.execute("select ?", (now,))
|
cur.execute("select ?", (now,))
|
||||||
print(cur.fetchone()[0])
|
print(cur.fetchone()[0])
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -14,3 +14,5 @@ cur = con.cursor()
|
||||||
p = Point(4.0, -3.2)
|
p = Point(4.0, -3.2)
|
||||||
cur.execute("select ?", (p,))
|
cur.execute("select ?", (p,))
|
||||||
print(cur.fetchone()[0])
|
print(cur.fetchone()[0])
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -15,3 +15,5 @@ cur = con.cursor()
|
||||||
p = Point(4.0, -3.2)
|
p = Point(4.0, -3.2)
|
||||||
cur.execute("select ?", (p,))
|
cur.execute("select ?", (p,))
|
||||||
print(cur.fetchone()[0])
|
print(cur.fetchone()[0])
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
con = sqlite3.connect("mydb")
|
|
|
@ -1,3 +0,0 @@
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
con = sqlite3.connect(":memory:")
|
|
|
@ -13,3 +13,5 @@ con = sqlite3.connect(":memory:", factory=CountCursorsConnection)
|
||||||
cur1 = con.cursor()
|
cur1 = con.cursor()
|
||||||
cur2 = con.cursor()
|
cur2 = con.cursor()
|
||||||
print(con.numcursors)
|
print(con.numcursors)
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -14,3 +14,7 @@ try:
|
||||||
con.execute("insert into person(firstname) values (?)", ("Joe",))
|
con.execute("insert into person(firstname) values (?)", ("Joe",))
|
||||||
except sqlite3.IntegrityError:
|
except sqlite3.IntegrityError:
|
||||||
print("couldn't add Joe twice")
|
print("couldn't add Joe twice")
|
||||||
|
|
||||||
|
# Connection object used as context manager only commits or rollbacks transactions,
|
||||||
|
# so the connection object should be closed manually
|
||||||
|
con.close()
|
||||||
|
|
|
@ -15,3 +15,5 @@ for (name_last, age) in cur:
|
||||||
cur.execute(SELECT)
|
cur.execute(SELECT)
|
||||||
for row in cur:
|
for row in cur:
|
||||||
print('%s is %d years old.' % (row[0], row[1]))
|
print('%s is %d years old.' % (row[0], row[1]))
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -11,3 +11,5 @@ cur.execute("select * from people order by age")
|
||||||
|
|
||||||
# Retrieve all rows as a sequence and print that sequence:
|
# Retrieve all rows as a sequence and print that sequence:
|
||||||
print(cur.fetchall())
|
print(cur.fetchall())
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -14,3 +14,5 @@ cur.execute("insert into people values (?, ?)", (who, age))
|
||||||
cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age})
|
cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age})
|
||||||
|
|
||||||
print(cur.fetchone())
|
print(cur.fetchone())
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
con = sqlite3.connect("mydb")
|
|
||||||
|
|
||||||
cur = con.cursor()
|
|
||||||
|
|
||||||
who = "Yeltsin"
|
|
||||||
age = 72
|
|
||||||
|
|
||||||
cur.execute("select name_last, age from people where name_last=:who and age=:age",
|
|
||||||
locals())
|
|
||||||
print(cur.fetchone())
|
|
|
@ -22,3 +22,5 @@ cur.executemany("insert into characters(c) values (?)", theIter)
|
||||||
|
|
||||||
cur.execute("select c from characters")
|
cur.execute("select c from characters")
|
||||||
print(cur.fetchall())
|
print(cur.fetchall())
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -13,3 +13,5 @@ cur.executemany("insert into characters(c) values (?)", char_generator())
|
||||||
|
|
||||||
cur.execute("select c from characters")
|
cur.execute("select c from characters")
|
||||||
print(cur.fetchall())
|
print(cur.fetchall())
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -22,3 +22,4 @@ cur.executescript("""
|
||||||
1987
|
1987
|
||||||
);
|
);
|
||||||
""")
|
""")
|
||||||
|
con.close()
|
||||||
|
|
|
@ -14,3 +14,5 @@ for person in newPeople:
|
||||||
|
|
||||||
# The changes will not be saved unless the transaction is committed explicitly:
|
# The changes will not be saved unless the transaction is committed explicitly:
|
||||||
con.commit()
|
con.commit()
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -24,3 +24,5 @@ con.executescript("""
|
||||||
""")
|
""")
|
||||||
for row in con.execute("select rowid, name, ingredients from recipe where name match 'pie'"):
|
for row in con.execute("select rowid, name, ingredients from recipe where name match 'pie'"):
|
||||||
print(row)
|
print(row)
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -9,3 +9,5 @@ con.create_function("md5", 1, md5sum)
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
cur.execute("select md5(?)", (b"foo",))
|
cur.execute("select md5(?)", (b"foo",))
|
||||||
print(cur.fetchone()[0])
|
print(cur.fetchone()[0])
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -18,3 +18,5 @@ cur.execute("insert into test(i) values (1)")
|
||||||
cur.execute("insert into test(i) values (2)")
|
cur.execute("insert into test(i) values (2)")
|
||||||
cur.execute("select mysum(i) from test")
|
cur.execute("select mysum(i) from test")
|
||||||
print(cur.fetchone()[0])
|
print(cur.fetchone()[0])
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -6,3 +6,5 @@ cur = con.cursor()
|
||||||
cur.execute('select ? as "x [timestamp]"', (datetime.datetime.now(),))
|
cur.execute('select ? as "x [timestamp]"', (datetime.datetime.now(),))
|
||||||
dt = cur.fetchone()[0]
|
dt = cur.fetchone()[0]
|
||||||
print(dt, type(dt))
|
print(dt, type(dt))
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -18,3 +18,5 @@ cur.execute('select current_date as "d [date]", current_timestamp as "ts [timest
|
||||||
row = cur.fetchone()
|
row = cur.fetchone()
|
||||||
print("current_date", row[0], type(row[0]))
|
print("current_date", row[0], type(row[0]))
|
||||||
print("current_timestamp", row[1], type(row[1]))
|
print("current_timestamp", row[1], type(row[1]))
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -11,3 +11,5 @@ con.row_factory = dict_factory
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
cur.execute("select 1 as a")
|
cur.execute("select 1 as a")
|
||||||
print(cur.fetchone()["a"])
|
print(cur.fetchone()["a"])
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -10,3 +10,5 @@ for row in cur:
|
||||||
assert row["name"] == row["nAmE"]
|
assert row["name"] == row["nAmE"]
|
||||||
assert row[1] == row["age"]
|
assert row[1] == row["age"]
|
||||||
assert row[1] == row["AgE"]
|
assert row[1] == row["AgE"]
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -18,3 +18,7 @@ for row in con.execute("select firstname, lastname from person"):
|
||||||
print(row)
|
print(row)
|
||||||
|
|
||||||
print("I just deleted", con.execute("delete from person").rowcount, "rows")
|
print("I just deleted", con.execute("delete from person").rowcount, "rows")
|
||||||
|
|
||||||
|
# close is not a shortcut method and it's not called automatically,
|
||||||
|
# so the connection object should be closed manually
|
||||||
|
con.close()
|
||||||
|
|
|
@ -24,3 +24,5 @@ for row in cur:
|
||||||
print(fieldValue.ljust(FIELD_MAX_WIDTH), end=' ')
|
print(fieldValue.ljust(FIELD_MAX_WIDTH), end=' ')
|
||||||
|
|
||||||
print() # Finish the row with a newline.
|
print() # Finish the row with a newline.
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -25,3 +25,5 @@ con.text_factory = lambda x: x.decode("utf-8") + "foo"
|
||||||
cur.execute("select ?", ("bar",))
|
cur.execute("select ?", ("bar",))
|
||||||
row = cur.fetchone()
|
row = cur.fetchone()
|
||||||
assert row[0] == "barfoo"
|
assert row[0] == "barfoo"
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
|
@ -537,6 +537,7 @@ Connection Objects
|
||||||
with open('dump.sql', 'w') as f:
|
with open('dump.sql', 'w') as f:
|
||||||
for line in con.iterdump():
|
for line in con.iterdump():
|
||||||
f.write('%s\n' % line)
|
f.write('%s\n' % line)
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
|
||||||
.. method:: backup(target, *, pages=0, progress=None, name="main", sleep=0.250)
|
.. method:: backup(target, *, pages=0, progress=None, name="main", sleep=0.250)
|
||||||
|
@ -573,8 +574,11 @@ Connection Objects
|
||||||
print(f'Copied {total-remaining} of {total} pages...')
|
print(f'Copied {total-remaining} of {total} pages...')
|
||||||
|
|
||||||
con = sqlite3.connect('existing_db.db')
|
con = sqlite3.connect('existing_db.db')
|
||||||
with sqlite3.connect('backup.db') as bck:
|
bck = sqlite3.connect('backup.db')
|
||||||
|
with bck:
|
||||||
con.backup(bck, pages=1, progress=progress)
|
con.backup(bck, pages=1, progress=progress)
|
||||||
|
bck.close()
|
||||||
|
con.close()
|
||||||
|
|
||||||
Example 2, copy an existing database into a transient copy::
|
Example 2, copy an existing database into a transient copy::
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue