Skip to content

Commit db20afe

Browse files
miss-islingtonErlend Egeberg Aasland
and
Erlend Egeberg Aasland
authored
bpo-44106: Improve sqlite3 example database contents (GH-26027)
(cherry picked from commit 92d1064) Co-authored-by: Erlend Egeberg Aasland <[email protected]>
1 parent 76ed53c commit db20afe

9 files changed

+37
-42
lines changed

Doc/includes/sqlite3/createdb.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
con = sqlite3.connect(DB_FILE)
1313
cur = con.cursor()
1414
cur.execute("""
15-
create table people
15+
create table lang
1616
(
17-
name_last varchar(20),
18-
age integer
17+
name varchar(20),
18+
first_appeared integer
1919
)
2020
""")
2121

22-
cur.execute("insert into people (name_last, age) values ('Yeltsin', 72)")
23-
cur.execute("insert into people (name_last, age) values ('Putin', 51)")
22+
cur.execute("insert into lang (name, first_appeared) values ('Forth', 1970)")
23+
cur.execute("insert into lang (name, first_appeared) values ('Ada', 1980)")
2424

2525
con.commit()
2626

Doc/includes/sqlite3/ctx_manager.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import sqlite3
22

33
con = sqlite3.connect(":memory:")
4-
con.execute("create table person (id integer primary key, firstname varchar unique)")
4+
con.execute("create table lang (id integer primary key, name varchar unique)")
55

66
# Successful, con.commit() is called automatically afterwards
77
with con:
8-
con.execute("insert into person(firstname) values (?)", ("Joe",))
8+
con.execute("insert into lang(name) values (?)", ("Python",))
99

1010
# con.rollback() is called after the with block finishes with an exception, the
1111
# exception is still raised and must be caught
1212
try:
1313
with con:
14-
con.execute("insert into person(firstname) values (?)", ("Joe",))
14+
con.execute("insert into lang(name) values (?)", ("Python",))
1515
except sqlite3.IntegrityError:
16-
print("couldn't add Joe twice")
16+
print("couldn't add Python twice")
1717

1818
# Connection object used as context manager only commits or rollbacks transactions,
1919
# so the connection object should be closed manually

Doc/includes/sqlite3/execsql_fetchonerow.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
con = sqlite3.connect("mydb")
44

55
cur = con.cursor()
6-
SELECT = "select name_last, age from people order by age, name_last"
6+
SELECT = "select name, first_appeared from people order by first_appeared, name"
77

88
# 1. Iterate over the rows available from the cursor, unpacking the
9-
# resulting sequences to yield their elements (name_last, age):
9+
# resulting sequences to yield their elements (name, first_appeared):
1010
cur.execute(SELECT)
11-
for (name_last, age) in cur:
12-
print('%s is %d years old.' % (name_last, age))
11+
for name, first_appeared in cur:
12+
print(f"The {name} programming language appeared in {first_appeared}.")
1313

1414
# 2. Equivalently:
1515
cur.execute(SELECT)
1616
for row in cur:
17-
print('%s is %d years old.' % (row[0], row[1]))
17+
print(f"The {row[0]} programming language appeared in {row[1]}.")
1818

1919
con.close()

Doc/includes/sqlite3/execsql_printall_1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
cur = con.cursor()
88

99
# Execute the SELECT statement:
10-
cur.execute("select * from people order by age")
10+
cur.execute("select * from lang order by first_appeared")
1111

1212
# Retrieve all rows as a sequence and print that sequence:
1313
print(cur.fetchall())

Doc/includes/sqlite3/execute_1.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22

33
con = sqlite3.connect(":memory:")
44
cur = con.cursor()
5-
cur.execute("create table lang (lang_name, lang_age)")
5+
cur.execute("create table lang (name, first_appeared)")
66

77
# This is the qmark style:
8-
cur.execute("insert into lang values (?, ?)", ("C", 49))
8+
cur.execute("insert into lang values (?, ?)", ("C", 1972))
99

1010
# The qmark style used with executemany():
1111
lang_list = [
12-
("Fortran", 64),
13-
("Python", 30),
14-
("Go", 11),
12+
("Fortran", 1957),
13+
("Python", 1991),
14+
("Go", 2009),
1515
]
1616
cur.executemany("insert into lang values (?, ?)", lang_list)
1717

1818
# And this is the named style:
19-
cur.execute("select * from lang where lang_name=:name and lang_age=:age",
20-
{"name": "C", "age": 49})
19+
cur.execute("select * from lang where first_appeared=:year", {"year": 1972})
2120
print(cur.fetchall())
2221

2322
con.close()

Doc/includes/sqlite3/insert_more_people.py renamed to Doc/includes/sqlite3/insert_more_langs.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
cur = con.cursor()
66

7-
newPeople = (
8-
('Lebed' , 53),
9-
('Zhirinovsky' , 57),
10-
)
7+
languages = (
8+
("Smalltalk", 1972),
9+
("Swift", 2014),
10+
)
1111

12-
for person in newPeople:
13-
cur.execute("insert into people (name_last, age) values (?, ?)", person)
12+
for lang in languages:
13+
cur.execute("insert into lang (name, first_appeared) values (?, ?)", lang)
1414

1515
# The changes will not be saved unless the transaction is committed explicitly:
1616
con.commit()

Doc/includes/sqlite3/shortcut_methods.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import sqlite3
22

3-
persons = [
4-
("Hugo", "Boss"),
5-
("Calvin", "Klein")
6-
]
3+
langs = [
4+
("C++", 1985),
5+
("Objective-C", 1984),
6+
]
77

88
con = sqlite3.connect(":memory:")
99

1010
# Create the table
11-
con.execute("create table person(firstname, lastname)")
11+
con.execute("create table lang(name, first_appeared)")
1212

1313
# Fill the table
14-
con.executemany("insert into person(firstname, lastname) values (?, ?)", persons)
14+
con.executemany("insert into lang(name, first_appeared) values (?, ?)", langs)
1515

1616
# Print the table contents
17-
for row in con.execute("select firstname, lastname from person"):
17+
for row in con.execute("select name, first_appeared from lang"):
1818
print(row)
1919

20-
print("I just deleted", con.execute("delete from person").rowcount, "rows")
20+
print("I just deleted", con.execute("delete from lang").rowcount, "rows")
2121

2222
# close is not a shortcut method and it's not called automatically,
2323
# so the connection object should be closed manually

Doc/includes/sqlite3/simple_tableprinter.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import sqlite3
22

33
FIELD_MAX_WIDTH = 20
4-
TABLE_NAME = 'people'
5-
SELECT = 'select * from %s order by age, name_last' % TABLE_NAME
64

75
con = sqlite3.connect("mydb")
8-
96
cur = con.cursor()
10-
cur.execute(SELECT)
7+
cur.execute("select * from lang order by name, first_appeared")
118

129
# Print a header.
1310
for fieldDesc in cur.description:

Doc/tools/susp-ignored.csv

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ library/smtplib,,:port,method must support that as well as a regular host:port
211211
library/socket,,::,'5aef:2b::8'
212212
library/socket,,:can,"return (can_id, can_dlc, data[:can_dlc])"
213213
library/socket,,:len,fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
214-
library/sqlite3,,:name,"cur.execute(""select * from lang where lang_name=:name and lang_age=:age"","
215-
library/sqlite3,,:age,"cur.execute(""select * from lang where lang_name=:name and lang_age=:age"","
214+
library/sqlite3,,:year,"cur.execute(""select * from lang where first_appeared=:year"", {""year"": 1972})"
216215
library/sqlite3,,:memory,
217216
library/sqlite3,,:path,"db = sqlite3.connect('file:path/to/database?mode=ro', uri=True)"
218217
library/ssl,,:My,"Organizational Unit Name (eg, section) []:My Group"

0 commit comments

Comments
 (0)