@@ -25,34 +25,34 @@ represents the database. Here the data will be stored in the
2525:file: `example.db ` file::
2626
2727 import sqlite3
28- conn = sqlite3.connect('example.db')
28+ con = sqlite3.connect('example.db')
2929
3030You can also supply the special name ``:memory: `` to create a database in RAM.
3131
3232Once you have a :class: `Connection `, you can create a :class: `Cursor ` object
3333and call its :meth: `~Cursor.execute ` method to perform SQL commands::
3434
35- c = conn .cursor()
35+ cur = con .cursor()
3636
3737 # 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)''')
4040
4141 # 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)")
4343
4444 # Save (commit) the changes
45- conn .commit()
45+ con .commit()
4646
4747 # We can also close the connection if we are done with it.
4848 # Just be sure any changes have been committed or they will be lost.
49- conn .close()
49+ con .close()
5050
5151The data you've saved is persistent and is available in subsequent sessions::
5252
5353 import sqlite3
54- conn = sqlite3.connect('example.db')
55- c = conn .cursor()
54+ con = sqlite3.connect('example.db')
55+ cur = con .cursor()
5656
5757Usually your SQL operations will need to use values from Python variables. You
5858shouldn't assemble your query using Python's string operations because doing so
@@ -67,19 +67,19 @@ example::
6767
6868 # Never do this -- insecure!
6969 symbol = 'RHAT'
70- c .execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
70+ cur .execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
7171
7272 # Do this instead
7373 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())
7676
7777 # Larger example that inserts many records at a time
7878 purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
7979 ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
8080 ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
8181 ]
82- c .executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
82+ cur .executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
8383
8484To retrieve data after executing a SELECT statement, you can either treat the
8585cursor as an :term: `iterator `, call the cursor's :meth: `~Cursor.fetchone ` method to
@@ -88,7 +88,7 @@ matching rows.
8888
8989This example uses the iterator form::
9090
91- >>> for row in c .execute('SELECT * FROM stocks ORDER BY price'):
91+ >>> for row in cur .execute('SELECT * FROM stocks ORDER BY price'):
9292 print(row)
9393
9494 ('2006-01-05', 'BUY', 'RHAT', 100, 35.14)
@@ -768,23 +768,23 @@ Row Objects
768768
769769Let's assume we initialize a table as in the example given above::
770770
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
774774 (date text, trans text, symbol text,
775775 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()
780780
781781Now we plug :class: `Row ` in::
782782
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')
786786 <sqlite3.Cursor object at 0x7f4e7dd8fa80>
787- >>> r = c .fetchone()
787+ >>> r = cur .fetchone()
788788 >>> type(r)
789789 <class 'sqlite3.Row'>
790790 >>> tuple(r)
0 commit comments