本文实例讲述了pythonsqlite的Row对象操作。分享给大家供大家参考,具体如下:一代码importsql
本文实例讲述了python sqlite的Row对象操作。分享给大家供大家参考,具体如下:
一 代码
import sqlite3
conn=sqlite3.connect("test.db")
c=conn.cursor()
c.execute("DROP TABLE stocks")
c.execute('''CREATE TABLE stocks(data text,trans text,symbol text,qty real,price real)
''')
c.execute("""INSERT INTO stocks VALUES('2017-01-05','BUY','RHAT',100,35.14)""")
conn.commit()
conn.row_factory=sqlite3.Row
c=conn.cursor()
c.execute('SELECT * FROM stocks')
r=c.fetchone()
print(type(r))
print(tuple(r))
print(r[2])
print(r.keys())
print(r['qty'])
for field in r:
print(field)
c.close()
二 运行结果
python sqlite Row对象py ======= <class 'sqlite3.Row'> ('2017-01-05', 'BUY', 'RHAT', 100.0, 35.14) RHAT ['data', 'trans', 'symbol', 'qty', 'price'] 100.0 2017-01-05 BUY RHAT 100.0 35.14