ループに問題があります。次のようなループがあります。
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
ここでの問題は、プログラムが 2 番目の cursor1.execute (アスタリスク * でマークされている) で、('SQL には 1 つのパラメーター マーカーが含まれていますが、4 つのパラメーターが指定されました'、'HY000') と文句を言うことi
です。社会保障番号であるため、1 桁の整数ではありませんが、この問題を解決する方法がわかりません。
編集:これは、印刷全体を含むループ全体です。
ループ (およびいくつかの定義)
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