8.1.3 Row对象
假设数据以下面的方式创建并插入数据:
1 import sqlite3
2
3 conn = sqlite3.connect(r'D:\test.db')
4 c = conn.cursor()
5 c.execute('create table stocks(date text,trans text,symbol text,qty real,price real)')
6 c.execute("insert into stocks values('2016-01-05','BUY','RHAT',100,35.14)")
7 conn.commit()
8 c.close()
9
10
11 #可以使用下面的方式里读取其中的数据
12 conn.row_factory = sqlite3.Row
13 c = conn.cursor()
14 c.execute('select * from stocks')
15 r = c.fetchone()
16 print(type(r))
17 print(tuple(r))
18 print(r[2])
19 print(r.keys())
20 print(r['qty'])
21
22 for field in r:
23 print(field)
24
25 '''
26 <class 'sqlite3.Row'>
27 ('2016-01-05', 'BUY', 'RHAT', 100.0, 35.14)
28 RHAT
29 ['date', 'trans', 'symbol', 'qty', 'price']
30 100.0
31 2016-01-05
32 BUY
33 RHAT
34 100.0
35 35.14
36 '''