pyodbc:('参数必须在列表,元组或行','HY000'中)与NumPy数据

时间:2017-02-16 23:28:51

标签: python pandas numpy pyodbc

我试图通过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)]

2 个答案:

答案 0 :(得分:4)

正如对问题的评论所述,问题在于

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)

答案 1 :(得分:0)

我认为重要的是要指出,如果您使用 tuple(__item__),您的字符串将被每个字符分隔...将您的字符串视为 C++ const char 指针。元组采用 const chars 指针项并将所有 elemetns 放入一个元组中。要阻止这种情况,您可以将字符串放入列表中,如下所示:tuple([__string__])。该列表不会留在元组中,它只是说将整个 const char 指针项作为一个元素放入我的元组中。希望这有帮助,最近在工作中遇到了这个问题。