【发布时间】:2020-03-10 10:29:59
【问题描述】:
我正在尝试使用cursor.executemany() 调整我的python 代码的INSERT INTO 性能,但我收到了一些错误。
代码:
import mysql.connector
mydb = mysql.connector.connect(
host="database1",
user="username1",
passwd="password1",
database="dbname"
)
data = [
'A',
'B',
'C',
]
mycursor = mydb.cursor()
query = "INSERT INTO table_name (name) VALUES (%s)"
mycursor.executemany(query, data)
mydb.commit()
根据the example,这应该可以,但是,我收到错误:
Traceback (most recent call last):
File "/Users/filipniko/PycharmProjects/scrape/darwin-scrape/venv/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 308, in _batch_insert
prepared = self._cnx.prepare_for_mysql(params)
File "/Users/filipniko/PycharmProjects/scrape/darwin-scrape/venv/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 632, in prepare_for_mysql
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "dbtest.py", line 18, in <module>
mycursor.executemany(query, data)
File "/Users/filipniko/PycharmProjects/scrape/darwin-scrape/venv/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 350, in executemany
stmt = self._batch_insert(operation, seq_params)
File "/Users/filipniko/PycharmProjects/scrape/darwin-scrape/venv/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 329, in _batch_insert
"Failed executing the operation; %s" % err)
mysql.connector.errors.InterfaceError: Failed executing the operation; Could not process parameters
注意事项:
- 表为空
- 我可以使用
mycursor.execute为列表中的每个项目添加行
表架构如下所示:
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | UNI | NULL | |
+-------+--------------+------+-----+---------+----------------+
我做错了什么?
【问题讨论】: