Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

sql server - Syntax for a parameter query in Python (pyodbc)

I am trying to write results of some processing into SQL Server table. My results are store in a list of lists where each item of the list is a list. I am using parameters (6 params) and I am getting the following error:

cnxn.execute(sqlStatement, (item[0],item[1],item[2],item[3],item[4],item[5])) pyodbc.ProgrammingError: ('The SQL contains 0 parameter markers, but 6 parameters were supplied', 'HY000')

That's my code

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()

Anyone knows why my execution fails ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should be using ? as parameter markers I believe.

Your sql should probably look like this:

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 (?, ?, ?, ?, ?, ?)"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...