@@ -26,34 +26,34 @@ represents the database. Here the data will be stored in the
26
26
:file: `example.db ` file::
27
27
28
28
import sqlite3
29
- conn = sqlite3.connect('example.db')
29
+ con = sqlite3.connect('example.db')
30
30
31
31
You can also supply the special name ``:memory: `` to create a database in RAM.
32
32
33
33
Once you have a :class: `Connection `, you can create a :class: `Cursor ` object
34
34
and call its :meth: `~Cursor.execute ` method to perform SQL commands::
35
35
36
- c = conn .cursor()
36
+ cur = con .cursor()
37
37
38
38
# 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)''')
41
41
42
42
# 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)")
44
44
45
45
# Save (commit) the changes
46
- conn .commit()
46
+ con .commit()
47
47
48
48
# We can also close the connection if we are done with it.
49
49
# Just be sure any changes have been committed or they will be lost.
50
- conn .close()
50
+ con .close()
51
51
52
52
The data you've saved is persistent and is available in subsequent sessions::
53
53
54
54
import sqlite3
55
- conn = sqlite3.connect('example.db')
56
- c = conn .cursor()
55
+ con = sqlite3.connect('example.db')
56
+ cur = con .cursor()
57
57
58
58
Usually your SQL operations will need to use values from Python variables. You
59
59
shouldn't assemble your query using Python's string operations because doing so
@@ -68,19 +68,19 @@ example::
68
68
69
69
# Never do this -- insecure!
70
70
symbol = 'RHAT'
71
- c .execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
71
+ cur .execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
72
72
73
73
# Do this instead
74
74
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())
77
77
78
78
# Larger example that inserts many records at a time
79
79
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
80
80
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
81
81
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
82
82
]
83
- c .executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
83
+ cur .executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
84
84
85
85
To retrieve data after executing a SELECT statement, you can either treat the
86
86
cursor as an :term: `iterator `, call the cursor's :meth: `~Cursor.fetchone ` method to
@@ -89,7 +89,7 @@ matching rows.
89
89
90
90
This example uses the iterator form::
91
91
92
- >>> for row in c .execute('SELECT * FROM stocks ORDER BY price'):
92
+ >>> for row in cur .execute('SELECT * FROM stocks ORDER BY price'):
93
93
print(row)
94
94
95
95
('2006-01-05', 'BUY', 'RHAT', 100, 35.14)
@@ -764,23 +764,23 @@ Row Objects
764
764
765
765
Let's assume we initialize a table as in the example given above::
766
766
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
770
770
(date text, trans text, symbol text,
771
771
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()
776
776
777
777
Now we plug :class: `Row ` in::
778
778
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')
782
782
<sqlite3.Cursor object at 0x7f4e7dd8fa80>
783
- >>> r = c .fetchone()
783
+ >>> r = cur .fetchone()
784
784
>>> type(r)
785
785
<class 'sqlite3.Row'>
786
786
>>> tuple(r)
0 commit comments