我遇到循环问题。我有一个循环如下:
for i in theList:
temp=process_query('SELECT FName, LName FROM Employee WHERE SSN=?', i)
known_hours=process_query('SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN=?',(i))
其中theList
定义为:
theList=process_query('SELECT DISTINCT SSN FROM Employee', None)
这现在可以正常运行,最后还要感谢熊市。但是我发现了另一个我无法修复的问题,即当我调用下面的process_query函数时:
def process_query(query, parameters):
if(parameters is None):
cursor1.execute(query)
else:
cursor1.execute(query, parameters) (*LINE 33)
n=0
lista = []
while 1:
row = cursor1.fetchone()
if not row:
break
lista.append(row[0])
n = n+1
return lista
麻烦现在是程序抱怨第二个cursor1.execute(标有星号,*)('SQL包含1个参数标记,但提供了4个参数','HY000')我相信从那个i
是社会安全号码,因此不是单个数字整数,但我无法理解如何解决这个问题。
编辑:这是整个循环的整个循环:
循环(以及一些定义)
theList=process_query('SELECT DISTINCT SSN FROM Employee', None)
temp=[]
for i in theList:
temp=process_query('SELECT FName, LName FROM Employee WHERE SSN= ?', (i)) (LINE 71)
known_hours=process_query('SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN=?',(i))
temp.append(known_hours)
unknown_hours=process_query('SELECT Distinct COUNT(*) FROM Works_On WHERE ISNULL(Hours) AND ESSN=?', (i))
unknown_hours=map(int,unknown_hours)
temp.append(unknown_hours)
if(unknown_hours > 0):
temp.append('Yes')
else:
temp.append('No')
theList.append(temp)
print theList
给出了以下打印(注意!我也更新了process_query,因此它打印出查询和参数(按此顺序)。
SELECT DISTINCT SSN FROM Employee
None
SELECT FName, LName FROM Employee WHERE SSN= ?
123456789
SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN=?
123456789
SELECT Distinct COUNT(*) FROM Works_On WHERE ISNULL(Hours) AND ESSN=?
123456789
SELECT FName, LName FROM Employee WHERE SSN= ?
333445555
SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN=?
333445555
SELECT Distinct COUNT(*) FROM Works_On WHERE ISNULL(Hours) AND ESSN=?
333445555
SELECT FName, LName FROM Employee WHERE SSN= ?
453453453
SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN=?
453453453
SELECT Distinct COUNT(*) FROM Works_On WHERE ISNULL(Hours) AND ESSN=?
453453453
SELECT FName, LName FROM Employee WHERE SSN= ?
666884444
SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN=?
666884444
SELECT Distinct COUNT(*) FROM Works_On WHERE ISNULL(Hours) AND ESSN=?
666884444
SELECT FName, LName FROM Employee WHERE SSN= ?
888665555
SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN=?
888665555
SELECT Distinct COUNT(*) FROM Works_On WHERE ISNULL(Hours) AND ESSN=?
888665555
SELECT FName, LName FROM Employee WHERE SSN= ?
987654321
SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN=?
987654321
SELECT Distinct COUNT(*) FROM Works_On WHERE ISNULL(Hours) AND ESSN=?
987654321
SELECT FName, LName FROM Employee WHERE SSN= ?
987987987
SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN=?
987987987
SELECT Distinct COUNT(*) FROM Works_On WHERE ISNULL(Hours) AND ESSN=?
987987987
SELECT FName, LName FROM Employee WHERE SSN= ?
999887777
SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN=?
999887777
SELECT Distinct COUNT(*) FROM Works_On WHERE ISNULL(Hours) AND ESSN=?
999887777
SELECT FName, LName FROM Employee WHERE SSN= ?
['John', [Decimal('40.0')], [0], 'Yes']
Traceback (most recent call last):
File "sumrpt2.py", line 72, in <module>
temp=process_query('SELECT FName, LName FROM Employee WHERE SSN= ?', (i))
File "sumrpt2.py", line 35, in process_query
cursor1.execute(query, parameters)
pyodbc.ProgrammingError: ('The SQL contains 1 parameter markers, but 4 parameters were supplied', 'HY000')
任何人都有线索?
此致 Cenderze
答案 0 :(得分:0)
我认为参数期待一个列表并且正在迭代这个数字。
尝试:
temp=process_query('SELECT FName, LName FROM Employee WHERE SSN= ?', [i])
传递清单。
答案 1 :(得分:0)
感谢beargle的评论,我终于可以找出问题所在!
我知道如果你自己遇到麻烦,谷歌这些答案有点困难,所以我想我可以简要总结一下这个错误的原因:
在这些帖子的评论中,已经给出了几种不同的方法来调用我认为应该起作用的函数。
然而,当我在List中循环'i'并且我实际上在循环中附加到它时我得到了错误“'SQL包含1个参数标记,但提供了4个参数','HY000'”。< / p>
我猜这是因为在我的查询中我只需要一个SSN,但由于我附加到了List,它会循环遍历不同的列,并且给我提供了比所需更多的标记(因此提供了4个参数,其中只需要1个参数标记)。
希望这可以帮助那些和我一样困难的人。
再次感谢beargle和其他花时间帮助我的人!
此致