我有一个循环问题。我有一个循环如下:
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)
这现在可以正常工作了,最后感谢beargle。但是我发现了另一个我无法解决的问题,即当我调用下面的 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
i
现在的问题是程序在第二个 cursor1.execute (标有星号,*)处抱怨('SQL 包含 1 个参数标记,但提供了 4 个参数','HY000'),我认为这是源于一个社会安全号码,因此不是一个数字整数,但我不明白如何解决这个问题。
编辑:这是带有整个打印的整个循环:
循环(和一些定义)
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