Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 运行更新SQL时pyodbc出现问题?_Python_Database_Pyodbc - Fatal编程技术网

Python 运行更新SQL时pyodbc出现问题?

Python 运行更新SQL时pyodbc出现问题?,python,database,pyodbc,Python,Database,Pyodbc,我正在尝试使用pyodbc在SQL server中升级。我有一个要更新/插入的列和值的列表。我已经尝试了我能想到的所有可能的选择,但无法解决这个问题。我的值在某些情况下是自由文本,也可以包含“'” 这就是我的代码的外观: def countList(lst1, lst2): return [sub[item] for item in range(len(lst2)) for sub in [lst1, lst2]] header_str

我正在尝试使用pyodbc在SQL server中升级。我有一个要更新/插入的列和值的列表。我已经尝试了我能想到的所有可能的选择,但无法解决这个问题。我的值在某些情况下是自由文本,也可以包含
“'”

这就是我的代码的外观:

def countList(lst1, lst2): 
    return [sub[item] for item in range(len(lst2)) 
                      for sub in [lst1, lst2]] 

header_string = ', '.join('[{0}]'.format(w) for w in incident_header)
list_for_update = (countList(incident_header, incident_values)) # 
##incident_values Example ###  ['123', 'true', '', '', '', '']
## list_for_update Example ###  ['number', '123', 'made_sla', 'true', 'closed_at', '', 'resolved_at', '', 'work_start', '', 'work_end', '']

k = len(incident_header)  # 6 - For above example
var_string = ', '.join('?' * len(incident_values))
my_string = "[?] = ?"
list_for_update_string = ",".join([my_string]*k)


query_string = "update LIVE_DATA set %s where number='%s';IF @@ROWCOUNT=0\
    INSERT INTO ICS_KAFKA_INCIDENT_LIVE_DATA (%s) values (%s);" % (list_for_update_string,number,header_string,var_string)
    
## This is how my SQL is gettting Formed: update ICS_KAFKA_INCIDENT_LIVE_DATA set [?] = ?,[?] = ?,[?] = ?,[?] = ?,[?] = ?,[?] = ? where number='123';IF @@ROWCOUNT=0    INSERT INTO ICS_KAFKA_INCIDENT_LIVE_DATA ([number], [made_sla], [closed_at], [resolved_at], [work_start], [work_end]) values (?, ?, ?, ?, ?, ?);

final_list = list_for_update + incident_values

cursor.execute(query_string,final_list)
我得到以下错误:

<ipython-input-106-62e3e93b4c08> in insert_and_update(number, incident_header, incident_values)
     33     print(check_sql_string(query_string,final_list))
     34 
---> 35     cursor.execute(query_string,final_list)
     36     connStr.commit()
     37 

ProgrammingError: ('The SQL contains 12 parameter markers, but 18 parameters were supplied', 'HY000')
插入和更新中的
(编号、事件标题、事件值)
33打印(检查sql字符串(查询字符串、最终列表))
34
--->35游标执行(查询字符串、最终列表)
36 connStr.commit()
37
编程错误:(“SQL包含12个参数标记,但提供了18个参数”,“HY000”)