问题
I am trying to insert a list of tuples into an MS SQL table via pyodbc with Python 3. This works:
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)])
but this does not:
params = list(row for row in data_table.round(0).head(10).to_records(index=False))
cursor.executemany(sql, params)
producing this error:
('Params must be in a list, tuple, or Row', 'HY000')
even though the output of params
is:
[('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)]
回答1:
As stated in the comments to the question, the issue was that
cursor.executemany(sql, params)
was failing because
params = list(row for row in data_table.round(0).head(10).to_records(index=False))
was not returning a list of "list[s], tuple[s], or [pyodbc] Row[s]", it was returning a list of "numpy.records". The solution was to convert the "records" so that params
contained a list of tuples:
params = list(tuple(row) for row in data_table.head(10).values)
来源:https://stackoverflow.com/questions/42286679/pyodbc-params-must-be-in-a-list-tuple-or-row-hy000-with-numpy-data