gpt4 book ai didi

python - pyodbc: ('Params must be in a list, tuple, or Row' , 'HY000' ) 与 NumPy 数据

转载 作者:行者123 更新时间:2023-11-28 21:43:24 30 4
gpt4 key购买 nike

我正在尝试使用 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))

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

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

关于python - pyodbc: ('Params must be in a list, tuple, or Row' , 'HY000' ) 与 NumPy 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42286679/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com