Skip to content

Commit 40d1b83

Browse files
author
Erlend Egeberg Aasland
authored
bpo-43396: Normalise naming in sqlite3 doc examples (pythonGH-24746)
1 parent e161ec5 commit 40d1b83

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

Doc/library/sqlite3.rst

+25-25
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,34 @@ represents the database. Here the data will be stored in the
2626
:file:`example.db` file::
2727

2828
import sqlite3
29-
conn = sqlite3.connect('example.db')
29+
con = sqlite3.connect('example.db')
3030

3131
You can also supply the special name ``:memory:`` to create a database in RAM.
3232

3333
Once you have a :class:`Connection`, you can create a :class:`Cursor` object
3434
and call its :meth:`~Cursor.execute` method to perform SQL commands::
3535

36-
c = conn.cursor()
36+
cur = con.cursor()
3737

3838
# Create table
39-
c.execute('''CREATE TABLE stocks
40-
(date text, trans text, symbol text, qty real, price real)''')
39+
cur.execute('''CREATE TABLE stocks
40+
(date text, trans text, symbol text, qty real, price real)''')
4141

4242
# Insert a row of data
43-
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
43+
cur.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
4444

4545
# Save (commit) the changes
46-
conn.commit()
46+
con.commit()
4747

4848
# We can also close the connection if we are done with it.
4949
# Just be sure any changes have been committed or they will be lost.
50-
conn.close()
50+
con.close()
5151

5252
The data you've saved is persistent and is available in subsequent sessions::
5353

5454
import sqlite3
55-
conn = sqlite3.connect('example.db')
56-
c = conn.cursor()
55+
con = sqlite3.connect('example.db')
56+
cur = con.cursor()
5757

5858
Usually your SQL operations will need to use values from Python variables. You
5959
shouldn't assemble your query using Python's string operations because doing so
@@ -68,19 +68,19 @@ example::
6868

6969
# Never do this -- insecure!
7070
symbol = 'RHAT'
71-
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
71+
cur.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
7272

7373
# Do this instead
7474
t = ('RHAT',)
75-
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
76-
print(c.fetchone())
75+
cur.execute('SELECT * FROM stocks WHERE symbol=?', t)
76+
print(cur.fetchone())
7777

7878
# Larger example that inserts many records at a time
7979
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
8080
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
8181
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
8282
]
83-
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
83+
cur.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
8484

8585
To retrieve data after executing a SELECT statement, you can either treat the
8686
cursor as an :term:`iterator`, call the cursor's :meth:`~Cursor.fetchone` method to
@@ -89,7 +89,7 @@ matching rows.
8989

9090
This example uses the iterator form::
9191

92-
>>> for row in c.execute('SELECT * FROM stocks ORDER BY price'):
92+
>>> for row in cur.execute('SELECT * FROM stocks ORDER BY price'):
9393
print(row)
9494

9595
('2006-01-05', 'BUY', 'RHAT', 100, 35.14)
@@ -764,23 +764,23 @@ Row Objects
764764

765765
Let's assume we initialize a table as in the example given above::
766766

767-
conn = sqlite3.connect(":memory:")
768-
c = conn.cursor()
769-
c.execute('''create table stocks
767+
con = sqlite3.connect(":memory:")
768+
cur = con.cursor()
769+
cur.execute('''create table stocks
770770
(date text, trans text, symbol text,
771771
qty real, price real)''')
772-
c.execute("""insert into stocks
773-
values ('2006-01-05','BUY','RHAT',100,35.14)""")
774-
conn.commit()
775-
c.close()
772+
cur.execute("""insert into stocks
773+
values ('2006-01-05','BUY','RHAT',100,35.14)""")
774+
con.commit()
775+
cur.close()
776776

777777
Now we plug :class:`Row` in::
778778

779-
>>> conn.row_factory = sqlite3.Row
780-
>>> c = conn.cursor()
781-
>>> c.execute('select * from stocks')
779+
>>> con.row_factory = sqlite3.Row
780+
>>> cur = con.cursor()
781+
>>> cur.execute('select * from stocks')
782782
<sqlite3.Cursor object at 0x7f4e7dd8fa80>
783-
>>> r = c.fetchone()
783+
>>> r = cur.fetchone()
784784
>>> type(r)
785785
<class 'sqlite3.Row'>
786786
>>> tuple(r)

0 commit comments

Comments
 (0)