2014-11-06 185 views
0

我正在嘗試將一些處理的結果寫入SQL Server表中。 我的結果存儲在列表中,列表中的每個項目都是列表。我正在使用參數(6 params),並且出現以下錯誤:Python中參數查詢的語法(pyodbc)

cnxn.execute(sqlStatement,(item [0],item [1],item [2],item [3],item [4 ]項[5])) pyodbc.ProgrammingError:( '的SQL包含0參數標記,但提供了6個參數', 'HY000')

這是我的代碼

sqlStatement = "INSERT INTO CEA_CR (`SessionID`, `Session.Call_TYPE_Assigned`, `Session.Did_Call_Type_happen_on_this_call`, `Session.Was_there_a_system_or_Rep_generated_Memo_that_matches_with_Call_Type` , 'cycle' , 'version') VALUES (%s, %s, %s, %s ,%s ,%s)" 

for item in result: 
    wr.writerow(item) 
    cnxn.execute(sqlStatement, (item[0],item[1],item[2],item[3],item[4],item[5])) 
    cnxn.commit() 

任何人都知道這是爲什麼我的執行失敗?

+2

我不知道你用的是什麼SQL驅動程序,但不應參數替換爲'',而不是'%s'? https://code.google.com/p/pyodbc/wiki/GettingStarted – RickyA 2014-11-06 13:37:13

回答

2

您應該使用?作爲參數標記我相信。

您的SQL或許應該是這樣的:?

sqlStatement = "INSERT INTO CEA_CR (SessionID, Session.Call_TYPE_Assigned, Session.Did_Call_Type_happen_on_this_call, Session.Was_there_a_system_or_Rep_generated_Memo_that_matches_with_Call_Type, cycle, version) VALUES (?, ?, ?, ?, ?, ?)" 
+0

我將我的語句更正爲:sqlStatement =「INSERT INTO CEA_CR('SessionID','Session.Call_TYPE_Assigned','Session.Did_Call_Type_happen_on_this_call',Session。 (?,?,?,?,?,?)「 但現在錯誤信息是:」''''附近的語法不正確'「 – user4045430 2014-11-06 13:56:07

+0

它在我看來像Nonnib回答你原來的問題。也許你應該接受這個答案,並問一個新的?如果您發佈了完整但最少的代碼示例,則更容易給出完全正確的答案。 – 2014-11-08 09:27:19

+0

在這種情況下通常使用的方法是嘗試並簡化所有內容,直到找出問題所在。刪除東西,直到它的工作。那時你就會有更多關於問題出現的信息。 – 2014-11-08 09:30:34