pyodbc: ('Params must be in a list, tuple, or Row', 'HY000') with NumPy data[英] pyodbc: ('Params must be in a list, tuple, or Row', 'HY000') with NumPy data

本文是小编为大家收集整理的关于pyodbc: ('Params must be in a list, tuple, or Row', 'HY000') with NumPy data的处理方法,想解了pyodbc: ('Params must be in a list, tuple, or Row', 'HY000') with NumPy data的问题怎么解决?pyodbc: ('Params must be in a list, tuple, or Row', 'HY000') with NumPy data问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我正在尝试使用 Python 3 通过 pyodbc 将元组列表插入 MS SQL 表中.这可行:

conn_string = (('DRIVER=FreeTDS;'
                'SERVER={};'
                'PORT=1433;'
                'DATABASE={};'
                'UID={};'
                'PWD={};'
                'TDS_Version=8.0;')
               .format(server, db, user, pwd))
sql = """
insert into Table1(field1, field2, field3) values (?, ?, ?)
"""

cursor.executemany(sql, [('1175B57E-7A10-4BAC-B22E-457C8266C0F2', '1D111FA5-A65D-4F77-A3AB-78B80BAF0C0B', 2),
 ('C60FA1C9-7656-4EBF-A577-44CBF0C641F4', '365FB706-9F7F-4ED4-AB88-927AE1F762A6', 1),
 ('2DF071CE-ABDF-4119-9573-2B0CA2B99C9C', '590E266D-810C-4DC2-BA5A-BAC05A9982C4', 0),
 ('EB794EF9-5FF4-49D3-B81E-F14F69C09306', '1D6E0C95-DF96-45D5-B8F3-211D825A160F', 0),
 ('EB794EF9-5FF4-49D3-B81E-F14F69C09306', '17D2EC66-845F-495D-9020-91E144B2E595', 0),
 ('EB794EF9-5FF4-49D3-B81E-F14F69C09306', '3A694B33-39F4-4F5B-899B-9B27810E4D82', 1),
 ('2DF071CE-ABDF-4119-9573-2B0CA2B99C9C', '4EE1D6F4-4A1E-4316-93D5-2AE1F461A193', 0),
 ('410DE087-1ADF-4EEA-BCE6-D7A1124E2A51', '17D2EC66-845F-495D-9020-91E144B2E595', 0),
 ('C60FA1C9-7656-4EBF-A577-44CBF0C641F4', '250256D4-4A3A-48E0-BCBA-7CD89011D81F', 1),
 ('2DF071CE-ABDF-4119-9573-2B0CA2B99C9C', '4D363718-FE0C-4257-8E4C-5C9A024BAEFF', 0)])

但事实并非如此:

params = list(row for row in data_table.round(0).head(10).to_records(index=False))

cursor.executemany(sql, params)

产生这个错误:

('Params must be in a list, tuple, or Row', 'HY000')

即使 params 的输出是:

[('1175B57E-7A10-4BAC-B22E-457C8266C0F2', '1D111FA5-A65D-4F77-A3AB-78B80BAF0C0B', 2),
     ('C60FA1C9-7656-4EBF-A577-44CBF0C641F4', '365FB706-9F7F-4ED4-AB88-927AE1F762A6', 1),
     ('2DF071CE-ABDF-4119-9573-2B0CA2B99C9C', '590E266D-810C-4DC2-BA5A-BAC05A9982C4', 0),
     ('EB794EF9-5FF4-49D3-B81E-F14F69C09306', '1D6E0C95-DF96-45D5-B8F3-211D825A160F', 0),
     ('EB794EF9-5FF4-49D3-B81E-F14F69C09306', '17D2EC66-845F-495D-9020-91E144B2E595', 0),
     ('EB794EF9-5FF4-49D3-B81E-F14F69C09306', '3A694B33-39F4-4F5B-899B-9B27810E4D82', 1),
     ('2DF071CE-ABDF-4119-9573-2B0CA2B99C9C', '4EE1D6F4-4A1E-4316-93D5-2AE1F461A193', 0),
     ('410DE087-1ADF-4EEA-BCE6-D7A1124E2A51', '17D2EC66-845F-495D-9020-91E144B2E595', 0),
     ('C60FA1C9-7656-4EBF-A577-44CBF0C641F4', '250256D4-4A3A-48E0-BCBA-7CD89011D81F', 1),
     ('2DF071CE-ABDF-4119-9573-2B0CA2B99C9C', '4D363718-FE0C-4257-8E4C-5C9A024BAEFF', 0)]

推荐答案

正如问题评论中所述,问题在于

cursor.executemany(sql, params)

失败是因为

params = list(row for row in data_table.round(0).head(10).to_records(index=False))

没有返回"list[s]、tuple[s] 或 [pyodbc] Row[s]"列表,而是返回"numpy.records"列表.解决方案是转换"记录",以便 params 包含一个元组列表:

params = list(tuple(row) for row in data_table.head(10).values)

本文地址:https://www.itbaoku.cn/post/1728177.html