假设数据以下面的方式创建并插入数据:
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.Row13 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 '''2627 ('2016-01-05', 'BUY', 'RHAT', 100.0, 35.14)28 RHAT29 ['date', 'trans', 'symbol', 'qty', 'price']30 100.031 2016-01-0532 BUY33 RHAT34 100.035 35.1436 '''