【发布时间】:2014-03-01 21:56:19
【问题描述】:
我正在尝试使用 python 的基本 sqlite 表返回多行。我从教程中稍微修改了代码:
import sqlite3
conn = sqlite3.connect('SQL_test_3.db') #this creates a seperate file
c = conn.cursor()
c.execute('drop table if exists stocks')
c.execute('''create table stocks
(date text, trans text, symbol text,
qty real, price real)''')
data = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
('0000-00-00', 'Test', 'Test', 100, 100)
]
for t in data:
c.execute('insert into stocks values (?,?,?,?,?)', t)
conn.commit()
price = 72
t = (price,)
c.execute('select * from stocks where price=? limit 2', t)
print c.fetchall()
我希望它返回:
[(u'2006-04-05', u'BUY', u'MSFT', 1000.0, 72.0)]
[(u'2006-04-06', u'SELL', u'IBM', 500.0, 53.0)]
我想过使用命令:
c.execute('select * from stocks where price=? limit 2', t)
但它只返回第一行。我尝试使用偏移量,但这似乎不起作用。有谁知道如何解决这个问题?如果有人可以提供帮助,将不胜感激。谢谢。
【问题讨论】:
-
@uʍopǝpısdn 是的,只返回一行。我希望返回多行 - 基于价格所在的位置。
标签: python sql database sqlite