繁体   English   中英

如何在 Python 中使用 executemany 更新 MySQL 表

[英]How do I update a MySQL table using executemany in Python

我正在尝试使用 executemany 将多个项目更新到 Python 中的 MySQL 表中。 我不确定我哪里出错了。

这是函数:

def update_bid_items(itemlines):
    c = create_connection()
    db = c[1]
    cur = db.cursor() 
    sql = """UPDATE bid_items            
             SET item_id = %s,
             description = %s,
             quantity = %s,
             base_price = %s,
             tier_price = %s,
             amount = %s
             WHERE bid_head_id = %s AND 
             row_num = %s
          """
    vals = (itemlines,)
    cur.executemany(sql, vals)        
    db.commit()
    db.close()

这是我传递给函数的数据:

[(4, '110V wall plug', '1', '5.00', '5.00', '5.00', '16', 0), (2, 'carpet nails box of 100', '1', '19.95', '19.95', '19.95', '16', 2)]

我收到以下错误:

_mysql_connector.MySQLInterfaceError: Python type tuple cannot be converted

我还用列表而不是元组尝试了这个错误:

_mysql_connector.MySQLInterfaceError: Python type list cannot be converted

itemlines已经是一个元组列表,你不需要把它放在另一个元组中。

所以摆脱vals并使用

cursor.executemany(sql, itemlines)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM