【问题标题】:Python "MySQL Error [-1]: Failed executing the operation; Could not process parameters"Python“MySQL 错误 [-1]:执行操作失败;无法处理参数”
【发布时间】:2021-08-13 21:20:54
【问题描述】:

我正在使用以下代码为我的表更新许多内容。但是,这在使用 Python 的 Windows 上工作很好,但在我的 Ubuntu 机器上却不行。更新很多人一直说MySQL Error [-1]: Failed executing the operation; Could not process parameters。是否有任何解决方案来追踪导致此错误的确切原因?

def update_wifs(done_data):
    magicwallet = mysql.connector.connect(
        host="localhost",
        user="dumpass",
        password="dumuser",
        database="magicwallet"
    )
    mysql_table = "magic97mil"
    conn = magicwallet.cursor()
    
    query = ""
    values = []

    for data_dict in done_data:

        if not query:
            columns = ', '.join('`{0}`'.format(k) for k in data_dict)
            duplicates = ', '.join('{0}=VALUES({0})'.format(k) for k in data_dict)
            place_holders = ', '.join('%s'.format(k) for k in data_dict)
            query = "INSERT INTO {0} ({1}) VALUES ({2})".format(mysql_table, columns, place_holders)
            query = "{0} ON DUPLICATE KEY UPDATE {1}".format(query, duplicates)

        v = data_dict.values()
        values.append(v)

    try:
        conn.executemany(query, values)
    except Exception as e:
        try:
            print("MySQL Error [%d]: %s" % (e.args[0], e.args[1]))
        except IndexError:
            print("MySQL Error: %s" % str(e))

        magicwallet.rollback()
        return False

    magicwallet.commit()
    conn.close()
    magicwallet.close()

    return done_data

done_data 就这样来了。我的表中列的名称与我的表中的列名完全相同。它在 Windows 机器上运行良好,但在 Unix 上一直出错

{'id': 73399, 'wif': 'uMx1VuwRT4cKQQyE', 'PublicAddress': 'p62GqtrDtRg', 'PublicAddressP2WPKH': 'dj3krprezquku7wkswv', 'phrase': '0075839', 'index_file': 73399, 'imported': 1}

{'id': 73400, 'wif': 'L1Ri4cv3vicfGbESct', 'PublicAddress': 'JfstH24WMHGZz63WEopk', 'PublicAddressP2WPKH': 'ffkt6xxgksktzzkq8ucydrn8ps', 'phrase': '007584', 'index_file': 73400, 'imported': 1}

UPD 0. 查询打印

INSERT INTO magic97mil (id, wif, PublicAddress, PublicAddressP2WPKH, phrase, index_file, imported) 值 (%s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE id=VALUES(id), wif=值(wif),公共地址=值(公共地址), 公共地址P2WPKH=值(公共地址P2WPKH), 短语=值(短语),索引文件=值(索引文件), 进口=VALUES(进口)

UPD 1. 完整追溯

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/mysql/connector/cursor_cext.py", line 313, in _batch_insert
    prepared = self._cnx.prepare_for_mysql(params)
  File "/usr/local/lib/python3.8/dist-packages/mysql/connector/connection_cext.py", line 659, 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 "/var/www/html/WalletMagic97mil/to_wif.py", line 76, in update_wifs
    conn.executemany(query, values)
  File "/usr/local/lib/python3.8/dist-packages/mysql/connector/cursor_cext.py", line 355, in executemany
    stmt = self._batch_insert(operation, seq_params)
  File "/usr/local/lib/python3.8/dist-packages/mysql/connector/cursor_cext.py", line 333, in _batch_insert
    raise errors.InterfaceError(
mysql.connector.errors.InterfaceError: Failed executing the operation; Could not process parameters

UPD 2. windows 和 unix 的区别values

所以print(values)在Unix和Windows上的区别是:

Windows 是这样的:

[{'id': 73401, 'wif': 'iVVUsHiLPLGq3uPaPyzdQ6xFeB3', 'PublicAddress': 'nLNtv3XUiMs7f1q8vU', 'PublicAddressP2WPKH': 'epw8ae08fnjpuva7x8783', 'phrase': '0075840', 'index_file': 73401, 'imported': 1}, 

{'id': 73402, 'wif': 'i41ZqWgvoKbUjYsbA41A', 'PublicAddress': 'Vd1D2krnjMucjmLU9', 'PublicAddressP2WPKH': '20g4my4rm4xgt04ph8xcmgyk', 'phrase': '0075841', 'index_file': 73402, 'imported': 1}]

Unix 是这样的:

[dict_values([73101, 'bmgHbonEKw4LoUqmwSg', '7K77mEtoiH5x8FnmJi2', 'dx3pdppq0zgacldefnge8ea3', '0075576', 73101, 1]),
 
dict_values([73102, 'nojKY4pzXxJ9TeFX14vpnk', 'qkuVECaPs3WcCj', 'j5sv9q28kzaqs0m6g', '0075577', 73102, 1])]

我认为这可能是确切的错误

【问题讨论】:

  • 打印query。我敢打赌问题会很明显。您还可以发布整个堆栈跟踪。
  • @MichaelRuth 我刚刚更新了打印查询中的问题。我现在将提供完整的回溯
  • @MichaelRuth 完成了完整的追溯
  • 那是你的问题:VALUES (%s, %s, %s, %s, %s, %s, %s), %s 不是有效值。
  • 您是否在两个平台上运行相同版本的 mysql-connector? (print(mysql.connector.__version__))。

标签: python mysql-connector


【解决方案1】:

您应该使用一种模式来构建查询:

        if not query:
            columns = ', '.join('`{0}`'.format(k) for k in data_dict)
            duplicates = ', '.join('{0}=VALUES({0})'.format(k) for k in data_dict)
            place_holders = ', '.join('`{0}`'.format(k) for k in data_dict)

确实,您不应该使用字符串构建查询。这是不安全、容易出错且不必要的。更好的方法是将parameter binding 与准备好的语句一起使用。

【讨论】:

  • 但是仍然一直说同样的错误。但是查询看起来很正常 INSERT INTO magic97mil (id, wif, PublicAddress, PublicAddressP2WPKH, phrase, index_file, imported) VALUES (id, @98765433@4, PublicAddressP2WPKH, phrase, index_file, imported) 重复密钥更新 id=VALUES(id), wif=VALUES(wif), PublicAddress=VALUES(PublicAddress), PublicAddressP2WPKH=VALUES(PublicAddressP2WPKH), 短语= VALUES(短语),index_file=VALUES(index_file),imported=VALUES(imported)
【解决方案2】:

我不知道为什么。但我像这样替换以下代码:

v = list(data_dict.values())
values.append(v)

它开始对我很好

【讨论】:

  • 在 Python2 中,dict.values() 返回一个 list。在 Python3 中,dict.values() 返回一个 view。因此,您似乎在 Windows 上使用 Python2,但在 Unix Python3 上。
猜你喜欢
  • 2021-10-04
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
相关资源
最近更新 更多