bpo-43396: Normalise naming in sqlite3 doc examples (GH-24746)

(cherry picked from commit 40d1b831ec)

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
This commit is contained in:
Miss Islington (bot) 2021-03-04 09:11:40 -08:00 committed by GitHub
parent 0e76157b0c
commit 374ee44933
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -25,34 +25,34 @@ represents the database. Here the data will be stored in the
:file:`example.db` file:: :file:`example.db` file::
import sqlite3 import sqlite3
conn = sqlite3.connect('example.db') con = sqlite3.connect('example.db')
You can also supply the special name ``:memory:`` to create a database in RAM. You can also supply the special name ``:memory:`` to create a database in RAM.
Once you have a :class:`Connection`, you can create a :class:`Cursor` object Once you have a :class:`Connection`, you can create a :class:`Cursor` object
and call its :meth:`~Cursor.execute` method to perform SQL commands:: and call its :meth:`~Cursor.execute` method to perform SQL commands::
c = conn.cursor() cur = con.cursor()
# Create table # Create table
c.execute('''CREATE TABLE stocks cur.execute('''CREATE TABLE stocks
(date text, trans text, symbol text, qty real, price real)''') (date text, trans text, symbol text, qty real, price real)''')
# Insert a row of data # Insert a row of data
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)") cur.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
# Save (commit) the changes # Save (commit) the changes
conn.commit() con.commit()
# We can also close the connection if we are done with it. # We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost. # Just be sure any changes have been committed or they will be lost.
conn.close() con.close()
The data you've saved is persistent and is available in subsequent sessions:: The data you've saved is persistent and is available in subsequent sessions::
import sqlite3 import sqlite3
conn = sqlite3.connect('example.db') con = sqlite3.connect('example.db')
c = conn.cursor() cur = con.cursor()
Usually your SQL operations will need to use values from Python variables. You Usually your SQL operations will need to use values from Python variables. You
shouldn't assemble your query using Python's string operations because doing so shouldn't assemble your query using Python's string operations because doing so
@ -67,19 +67,19 @@ example::
# Never do this -- insecure! # Never do this -- insecure!
symbol = 'RHAT' symbol = 'RHAT'
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol) cur.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
# Do this instead # Do this instead
t = ('RHAT',) t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t) cur.execute('SELECT * FROM stocks WHERE symbol=?', t)
print(c.fetchone()) print(cur.fetchone())
# Larger example that inserts many records at a time # Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00), purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00), ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00), ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
] ]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases) cur.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
To retrieve data after executing a SELECT statement, you can either treat the To retrieve data after executing a SELECT statement, you can either treat the
cursor as an :term:`iterator`, call the cursor's :meth:`~Cursor.fetchone` method to cursor as an :term:`iterator`, call the cursor's :meth:`~Cursor.fetchone` method to
@ -88,7 +88,7 @@ matching rows.
This example uses the iterator form:: This example uses the iterator form::
>>> for row in c.execute('SELECT * FROM stocks ORDER BY price'): >>> for row in cur.execute('SELECT * FROM stocks ORDER BY price'):
print(row) print(row)
('2006-01-05', 'BUY', 'RHAT', 100, 35.14) ('2006-01-05', 'BUY', 'RHAT', 100, 35.14)
@ -768,23 +768,23 @@ Row Objects
Let's assume we initialize a table as in the example given above:: Let's assume we initialize a table as in the example given above::
conn = sqlite3.connect(":memory:") con = sqlite3.connect(":memory:")
c = conn.cursor() cur = con.cursor()
c.execute('''create table stocks cur.execute('''create table stocks
(date text, trans text, symbol text, (date text, trans text, symbol text,
qty real, price real)''') qty real, price real)''')
c.execute("""insert into stocks cur.execute("""insert into stocks
values ('2006-01-05','BUY','RHAT',100,35.14)""") values ('2006-01-05','BUY','RHAT',100,35.14)""")
conn.commit() con.commit()
c.close() cur.close()
Now we plug :class:`Row` in:: Now we plug :class:`Row` in::
>>> conn.row_factory = sqlite3.Row >>> con.row_factory = sqlite3.Row
>>> c = conn.cursor() >>> cur = con.cursor()
>>> c.execute('select * from stocks') >>> cur.execute('select * from stocks')
<sqlite3.Cursor object at 0x7f4e7dd8fa80> <sqlite3.Cursor object at 0x7f4e7dd8fa80>
>>> r = c.fetchone() >>> r = cur.fetchone()
>>> type(r) >>> type(r)
<class 'sqlite3.Row'> <class 'sqlite3.Row'>
>>> tuple(r) >>> tuple(r)