Python中参数查询的语法(pyodbc) [英] Syntax for a parameter query in Python (pyodbc)

查看:372
本文介绍了Python中参数查询的语法(pyodbc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将某些处理的结果写入SQL Server表中. 我的结果存储在列表列表中,其中列表的每个项目都是一个列表.我正在使用参数(6个参数),但出现以下错误:

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 :("SQL包含0个参数标记,但提供了6个参数","HY000")

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

那是我的代码

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 ?

推荐答案

您应该使用?作为我相信的参数标记.

You should be using ? as parameter markers I believe.

您的sql应该看起来像这样:

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

这篇关于Python中参数查询的语法(pyodbc)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆