python - Executemany 因“执行操作失败;无法处理参数”而失败
问题描述
我正在尝试使用 调整INSERT INTO
我的 python 代码的性能cursor.executemany()
,但我收到了一些错误。
编码:
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()
根据示例,这应该可以工作,但是,我遇到了错误:
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 | |
+-------+--------------+------+-----+---------+----------------+
我究竟做错了什么?
解决方案
我发现我使用了错误的依赖项,我应该使用mySQLdb
而不是mysql.connector
:
import MySQLdb as my
mydb = my.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()
推荐阅读
- typescript - nestjs - neovim [import-prefix-missing no-local: Unable to load a local module] 错误
- javascript - slick-carousel 不适用于 MVC 的部分视图
- css - Angular + Bootstrap:将 Typescript 中的参数传递给样式表,并在 @if 中使用它来确定样式
- javascript - 如何给我的添加到购物车按钮自定义行为?
- javascript - 两个函数在被调用时都需要是异步的还是只是父函数?
- c# - 哪个 .config 文件用于测试 dll?
- r - 如何拆分使用 geom_boxplot 制作的箱线图?
- mysql - 使用连接显示mysql中两个表之间的差异
- php - Google 图表未使用 mpdf 和 codeigniter 3.x 以 pdf 格式导出
- debugging - Windbg:使用“dt”命令时指定伪寄存器有什么好处