如何修复我的代码以使用 Python 3.7 插入 SQL 服务器
How do I fix my code for insert into SQL Server with Python 3.7
我尝试使用 Python37 (pymssql) 将数据插入 SQL 服务器 table,但出现如下所示的错误。你有什么建议吗?
这些是要插入到 table
中的值
values = ['test','test', 'test', 'test', 'test', 'test','test','test','test','test','test','test','test','test']
SQL python 代码中的字符串
insSQL = '''INSERT INTO [dbo].[S_LAZ_GET_ORDERS](ORDER_ID,
ORDER_NUMBER,
BILL_ADDRESS1,
BILL_ADDRESS2,
BILL_ADDRESS3,
BILL_ADDRESS4,
BILL_ADDRESS5,
BILL_CITY,
BILL_COUNTRY,
BILL_FIRST_NAME,
BILL_LAST_NAME,
BILL_PHONE,
BILL_PHONE2,
BILL_POST_CODE)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'''
cur.executemany(insSQL, values)
cur.commit()
这是我得到的错误:
Traceback (most recent call last):
File "src_mssql.pyx", line 1930, in _mssql._substitute_params
IndexError: tuple index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python Project\Lazada API\getOrder.py", line 125, in getorders()
File "D:\Python Project\Lazada API\getOrder.py", line 109, in getorders
cur.executemany(insSQL, values)
File "src\pymssql.pyx", line 476, in pymssql.Cursor.executemany
File "src\pymssql.pyx", line 450, in pymssql.Cursor.execute
File "src_mssql.pyx", line 1070, in _mssql.MSSQLConnection.execute_query
File "src_mssql.pyx", line 1101, in _mssql.MSSQLConnection.execute_query
File "src_mssql.pyx", line 1218, in _mssql.MSSQLConnection.format_and_run_query
File "src_mssql.pyx", line 1240, in _mssql.MSSQLConnection.format_sql_command
File "src_mssql.pyx", line 1932, in _mssql._substitute_params
ValueError: more placeholders in SQL than params available
列表values
应该是一个参数序列(values
中的每个元素都是要插入的一行)。因此,如果您想在 table 中插入一行,其中行中的每个单元格都具有值 'test',那么只需将一个 14 元素的元组放入 values 中,如下所示:
values = [('test','test', 'test', 'test', 'test', 'test','test','test','test','test','test','test','test','test')]
如果要添加更多行,只需将更多元组添加到 values
。
我尝试使用 Python37 (pymssql) 将数据插入 SQL 服务器 table,但出现如下所示的错误。你有什么建议吗?
这些是要插入到 table
中的值values = ['test','test', 'test', 'test', 'test', 'test','test','test','test','test','test','test','test','test']
SQL python 代码中的字符串
insSQL = '''INSERT INTO [dbo].[S_LAZ_GET_ORDERS](ORDER_ID,
ORDER_NUMBER,
BILL_ADDRESS1,
BILL_ADDRESS2,
BILL_ADDRESS3,
BILL_ADDRESS4,
BILL_ADDRESS5,
BILL_CITY,
BILL_COUNTRY,
BILL_FIRST_NAME,
BILL_LAST_NAME,
BILL_PHONE,
BILL_PHONE2,
BILL_POST_CODE)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'''
cur.executemany(insSQL, values)
cur.commit()
这是我得到的错误:
Traceback (most recent call last):
File "src_mssql.pyx", line 1930, in _mssql._substitute_params
IndexError: tuple index out of rangeDuring handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python Project\Lazada API\getOrder.py", line 125, in getorders()
File "D:\Python Project\Lazada API\getOrder.py", line 109, in getorders
cur.executemany(insSQL, values)File "src\pymssql.pyx", line 476, in pymssql.Cursor.executemany
File "src\pymssql.pyx", line 450, in pymssql.Cursor.execute
File "src_mssql.pyx", line 1070, in _mssql.MSSQLConnection.execute_query
File "src_mssql.pyx", line 1101, in _mssql.MSSQLConnection.execute_query
File "src_mssql.pyx", line 1218, in _mssql.MSSQLConnection.format_and_run_query
File "src_mssql.pyx", line 1240, in _mssql.MSSQLConnection.format_sql_command
File "src_mssql.pyx", line 1932, in _mssql._substitute_paramsValueError: more placeholders in SQL than params available
列表values
应该是一个参数序列(values
中的每个元素都是要插入的一行)。因此,如果您想在 table 中插入一行,其中行中的每个单元格都具有值 'test',那么只需将一个 14 元素的元组放入 values 中,如下所示:
values = [('test','test', 'test', 'test', 'test', 'test','test','test','test','test','test','test','test','test')]
如果要添加更多行,只需将更多元组添加到 values
。