【问题标题】:Executemany is failing with "Failed executing the operation; Could not process parameters"Executemany 因“执行操作失败;无法处理参数”而失败
【发布时间】: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    |                |
+-------+--------------+------+-----+---------+----------------+

我做错了什么?

【问题讨论】:

    标签: python mysql


    【解决方案1】:

    我发现我使用了错误的依赖项,我应该使用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()
    

    https://thepythonguru.com/inserting-rows/

    【讨论】:

      猜你喜欢
      • 2019-10-03
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2021-09-22
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多