【问题标题】:Python: insert list of list values into mysql databasePython:将列表值列表插入mysql数据库
【发布时间】:2020-10-12 10:27:59
【问题描述】:

我有一个列表值列表,我想添加到 mysql 数据库中。

技术 = [1.0, 1.0, '', 0.92, 0.65, 1.0, 0.83, '', 0.5] hr = [1.0, 2.0, 1.5, 4.0, .83, 1.2]

mysql 数据库具有与列表变量相同的列名。你能指导我如何实现它吗?

我尝试如下但收到错误“并非所有参数都在 sql 语句中使用:

query = "INSERT INTO statvalues (tech, hr) VALUES (%s, %s)"
values = (tech, hr)
cursor.executemany(query,values)
cursor.close()
database.commit()

【问题讨论】:

标签: python mysql


【解决方案1】:

根据此链接上的答案:MySql: Inserting python lists into a table,我能够解决我的问题

query = "INSERT INTO statvalues (tech, hr) values (%s, %s)"
cursor.executemany(query, [(x, y) for x, y in zip(techlist, hrlist)])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-08
    • 2020-02-06
    • 2012-08-31
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2020-08-15
    • 2020-12-12
    相关资源
    最近更新 更多