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