]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44106: Improve sqlite3 example database contents (GH-26027)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 19 May 2021 07:43:44 +0000 (00:43 -0700)
committerGitHub <noreply@github.com>
Wed, 19 May 2021 07:43:44 +0000 (10:43 +0300)
(cherry picked from commit 92d1064727d6b7f4136f9c09ab52ae15e3676afe)

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
Doc/includes/sqlite3/createdb.py
Doc/includes/sqlite3/ctx_manager.py
Doc/includes/sqlite3/execsql_fetchonerow.py
Doc/includes/sqlite3/execsql_printall_1.py
Doc/includes/sqlite3/execute_1.py
Doc/includes/sqlite3/insert_more_langs.py [moved from Doc/includes/sqlite3/insert_more_people.py with 50% similarity]
Doc/includes/sqlite3/shortcut_methods.py
Doc/includes/sqlite3/simple_tableprinter.py
Doc/tools/susp-ignored.csv

index ee2950bdf8164610cb0bc4cb4074b9bc406078c6..49702121f72534c016ec357414d9e7270f83e034 100644 (file)
@@ -12,15 +12,15 @@ if os.path.exists(DB_FILE):
 con = sqlite3.connect(DB_FILE)
 cur = con.cursor()
 cur.execute("""
-        create table people
+        create table lang
         (
-          name_last      varchar(20),
-          age            integer
+          name           varchar(20),
+          first_appeared integer
         )
         """)
 
-cur.execute("insert into people (name_last, age) values ('Yeltsin',   72)")
-cur.execute("insert into people (name_last, age) values ('Putin',     51)")
+cur.execute("insert into lang (name, first_appeared) values ('Forth', 1970)")
+cur.execute("insert into lang (name, first_appeared) values ('Ada', 1980)")
 
 con.commit()
 
index 6db77d45046e1f6676eece35b21ee400387bf535..2e1175ef44c64151a63791c83a005b9d323c1e02 100644 (file)
@@ -1,19 +1,19 @@
 import sqlite3
 
 con = sqlite3.connect(":memory:")
-con.execute("create table person (id integer primary key, firstname varchar unique)")
+con.execute("create table lang (id integer primary key, name varchar unique)")
 
 # Successful, con.commit() is called automatically afterwards
 with con:
-    con.execute("insert into person(firstname) values (?)", ("Joe",))
+    con.execute("insert into lang(name) values (?)", ("Python",))
 
 # con.rollback() is called after the with block finishes with an exception, the
 # exception is still raised and must be caught
 try:
     with con:
-        con.execute("insert into person(firstname) values (?)", ("Joe",))
+        con.execute("insert into lang(name) values (?)", ("Python",))
 except sqlite3.IntegrityError:
-    print("couldn't add Joe twice")
+    print("couldn't add Python twice")
 
 # Connection object used as context manager only commits or rollbacks transactions,
 # so the connection object should be closed manually
index 115bcb50c7c754ff992ebe5cbf58e266ed8edfed..0ca7e14469760b9fde498c9167bb5db320ac5cf8 100644 (file)
@@ -3,17 +3,17 @@ import sqlite3
 con = sqlite3.connect("mydb")
 
 cur = con.cursor()
-SELECT = "select name_last, age from people order by age, name_last"
+SELECT = "select name, first_appeared from people order by first_appeared, name"
 
 # 1. Iterate over the rows available from the cursor, unpacking the
-# resulting sequences to yield their elements (name_last, age):
+# resulting sequences to yield their elements (name, first_appeared):
 cur.execute(SELECT)
-for (name_last, age) in cur:
-    print('%s is %d years old.' % (name_last, age))
+for name, first_appeared in cur:
+    print(f"The {name} programming language appeared in {first_appeared}.")
 
 # 2. Equivalently:
 cur.execute(SELECT)
 for row in cur:
-    print('%s is %d years old.' % (row[0], row[1]))
+    print(f"The {row[0]} programming language appeared in {row[1]}.")
 
 con.close()
index 19306e6e3ca7d1882b67228a76ed3f8c40cf558d..b3b42b5567df3b77715e1bf81819d21cf599f116 100644 (file)
@@ -7,7 +7,7 @@ con = sqlite3.connect("mydb")
 cur = con.cursor()
 
 # Execute the SELECT statement:
-cur.execute("select * from people order by age")
+cur.execute("select * from lang order by first_appeared")
 
 # Retrieve all rows as a sequence and print that sequence:
 print(cur.fetchall())
index 42aad4d5839f06ebe3e07619017341ce5bf799ab..ee0000e2b94a3241fddc8237ff915cf99bb7fb42 100644 (file)
@@ -2,22 +2,21 @@ import sqlite3
 
 con = sqlite3.connect(":memory:")
 cur = con.cursor()
-cur.execute("create table lang (lang_name, lang_age)")
+cur.execute("create table lang (name, first_appeared)")
 
 # This is the qmark style:
-cur.execute("insert into lang values (?, ?)", ("C", 49))
+cur.execute("insert into lang values (?, ?)", ("C", 1972))
 
 # The qmark style used with executemany():
 lang_list = [
-    ("Fortran", 64),
-    ("Python", 30),
-    ("Go", 11),
+    ("Fortran", 1957),
+    ("Python", 1991),
+    ("Go", 2009),
 ]
 cur.executemany("insert into lang values (?, ?)", lang_list)
 
 # And this is the named style:
-cur.execute("select * from lang where lang_name=:name and lang_age=:age",
-            {"name": "C", "age": 49})
+cur.execute("select * from lang where first_appeared=:year", {"year": 1972})
 print(cur.fetchall())
 
 con.close()
similarity index 50%
rename from Doc/includes/sqlite3/insert_more_people.py
rename to Doc/includes/sqlite3/insert_more_langs.py
index 10cf937243f6dac5f684e920960e2ab575e5338d..ceef949608449e9eda88a7723adb0f928c765c47 100644 (file)
@@ -4,13 +4,13 @@ con = sqlite3.connect("mydb")
 
 cur = con.cursor()
 
-newPeople = (
-    ('Lebed'       , 53),
-    ('Zhirinovsky' , 57),
-  )
+languages = (
+    ("Smalltalk", 1972),
+    ("Swift", 2014),
+)
 
-for person in newPeople:
-    cur.execute("insert into people (name_last, age) values (?, ?)", person)
+for lang in languages:
+    cur.execute("insert into lang (name, first_appeared) values (?, ?)", lang)
 
 # The changes will not be saved unless the transaction is committed explicitly:
 con.commit()
index 98a39411495cbac9e55bc8c6adddd0fb69447e51..48ea6fad15a89853835675485dd43cfbe3e6c3bb 100644 (file)
@@ -1,23 +1,23 @@
 import sqlite3
 
-persons = [
-    ("Hugo", "Boss"),
-    ("Calvin", "Klein")
-    ]
+langs = [
+    ("C++", 1985),
+    ("Objective-C", 1984),
+]
 
 con = sqlite3.connect(":memory:")
 
 # Create the table
-con.execute("create table person(firstname, lastname)")
+con.execute("create table lang(name, first_appeared)")
 
 # Fill the table
-con.executemany("insert into person(firstname, lastname) values (?, ?)", persons)
+con.executemany("insert into lang(name, first_appeared) values (?, ?)", langs)
 
 # Print the table contents
-for row in con.execute("select firstname, lastname from person"):
+for row in con.execute("select name, first_appeared from lang"):
     print(row)
 
-print("I just deleted", con.execute("delete from person").rowcount, "rows")
+print("I just deleted", con.execute("delete from lang").rowcount, "rows")
 
 # close is not a shortcut method and it's not called automatically,
 # so the connection object should be closed manually
index 148a1707f948bc5013fdb0ec3c4d9f813d6e4ca8..9be6e4f414acd8270b89468c57b281042df2cba8 100644 (file)
@@ -1,13 +1,10 @@
 import sqlite3
 
 FIELD_MAX_WIDTH = 20
-TABLE_NAME = 'people'
-SELECT = 'select * from %s order by age, name_last' % TABLE_NAME
 
 con = sqlite3.connect("mydb")
-
 cur = con.cursor()
-cur.execute(SELECT)
+cur.execute("select * from lang order by name, first_appeared")
 
 # Print a header.
 for fieldDesc in cur.description:
index 95277c44129cc987322f208ff79be77b51ec63da..ad5f55e8038520810ffb55c1053dcd5a188f9810 100644 (file)
@@ -211,8 +211,7 @@ library/smtplib,,:port,method must support that as well as a regular host:port
 library/socket,,::,'5aef:2b::8'
 library/socket,,:can,"return (can_id, can_dlc, data[:can_dlc])"
 library/socket,,:len,fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
-library/sqlite3,,:name,"cur.execute(""select * from lang where lang_name=:name and lang_age=:age"","
-library/sqlite3,,:age,"cur.execute(""select * from lang where lang_name=:name and lang_age=:age"","
+library/sqlite3,,:year,"cur.execute(""select * from lang where first_appeared=:year"", {""year"": 1972})"
 library/sqlite3,,:memory,
 library/sqlite3,,:path,"db = sqlite3.connect('file:path/to/database?mode=ro', uri=True)"
 library/ssl,,:My,"Organizational Unit Name (eg, section) []:My Group"