2015-11-06 127 views
0
select=input("select: ") 
    for row in data.execute('select ? from stocks where date like "2015-11-05" ',(select)): 

     print(row) 

这就是我现在要做的所有事情,但是我得到这个错误, t找到解决方案python sql,“select?from table where?like?”,(selected,where,like))

sqlite3.ProgrammingError: Incorrect number of bindings supplied. 
    The current statement uses 1, and there are 5 supplied. 

有没有办法做到这一点?我假设答案与标题类似。

回答

0

(select)不是一个元组,它是一个字符串,在你的情况下是一个由5个字符组成的字符串。由于字符串也是可迭代的,因此sqlite会将字符串拆分为字符并尝试使用字符串中的所有5个字符对查询进行参数化。相反,你的意思是有内部的单个元素的元组:

data.execute('select ? from stocks where date like "2015-11-05" ', (select,)) 

但是,问题是 - 这是行不通的,你cannot parameterize the table or column names,你不得不使用字符串格式化:

data.execute('select {} from stocks where date like "2015-11-05"'.format(select)) 

请注意,由于我们在此处使用字符串格式,因此我们正在使代码容易受到SQL注入的影响 - 您应该肯定验证select变量值并保护自己免受SQL注入(不确定在您的情况下谁将成为程序的用户虽然)。

+0

@ user5464854很高兴帮助,请参阅http://stackoverflow.com/help/someone-answers。 – alecxe