【问题标题】:Upsert in mariadb/mysqldb在 mariadb/mysqldb 中插入
【发布时间】:2019-10-03 16:54:03
【问题描述】:
I have created a table as below:

    +------------------+--------------+------+-----+------------+----------------+
    | Field            | Type         | Null | Key | Default    | Extra          |
    +------------------+--------------+------+-----+------------+----------------+
    | category         | varchar(20)  | NO   |     | NULL       |                |
    | report_id        | int(5)       | NO   | PRI | NULL       | auto_increment |
    | name             | varchar(255) | NO   | UNI | NULL       |                |
    | URL              | varchar(200) | NO   |     | NULL       |                |
    | refresh_type     | varchar(30)  | NO   |     | On Request |                |
    | create_dt        | date         | NO   |     | 9999-01-01 |                |
    | modified_dt      | date         | NO   |     | 9999-01-01 |                |
    | project_type     | varchar(60)  | YES  |     | NULL       |                |
    | project_name     | varchar(60)  | YES  |     | NULL       |                |
    | project_location | varchar(255) | YES  |     | NULL       |                |
    +------------------+--------------+------+-----+------------+----------------+

我也插入了记录。我正在尝试自动化插入和维护的过程,但我只从数据馈送中获得类别、名称和 URL 等几列。

根据流程,用户可以更新记录并更改其他字段以使其有用,并且下次尝试插入记录时,我想根据名称执行 upsert。我正在使用 python 执行该过程。以下是我尝试的步骤:

        dash= df.loc[:,['folder','name','url','url']].values.tolist()

        dash_insert_sql= ("""insert into dashboards (category 
                                                   ,name
                                                   ,URL
                                                   ) values (%s,%s,%s)
                                                   on duplicate key update URL = values(%s) """)

        cur.executemany(dash_insert_sql, dash)

当我尝试这个时,我得到了

    Traceback (most recent call last):
      File "C:\Python\Mysql\report_uri.py", line 65, in <module>
        cur.executemany(dash_insert_sql, dash)
      File "C:\Python\lib\site-packages\MySQLdb\cursors.py", line 228, in executemany
        q_prefix = m.group(1) % ()
    TypeError: not enough arguments for format string

这是示例数据:

我的输入是一个列表列表。

[['学生信息', '未安排的活跃学生', 'https://example.com/SASReportViewer/?reportUri=/reports/reports/af4f7325-860f-4958-ad83-bb900f726b32&page=vi6', 'https://example.com/SASReportViewer/?reportUri=/reports/reports/af4f7325-860f-4958-ad83-bb900f726b32&page=vi6'], ['学生信息', '来自快照的招生统计比较', 'https://example.com/SASReportViewer/?reportUri=/reports/reports/6150909f-3ab4-4ec7-8ef0-7efdb1f09300&page=vi6', 'https://example.com/SASReportViewer/?reportUri=/reports/reports/6150909f-3ab4-4ec7-8ef0-7efdb1f09300&page=vi6']]

请让我知道如何进行或哪里出错了。谢谢。

【问题讨论】:

标签: mysql python-3.x mariadb


【解决方案1】:

dash= df.loc[:,['folder','name','url']].values.tolist()

    dash_insert_sql= ("""insert into dashboards (category 
                                               ,name
                                               ,URL
                                               ) values (%s,%s,%s)
                                               on duplicate key update URL = values(URL) """)

我通过将报告的 url 更改为另一个报告的 url 对其进行了测试,它确实更新了 url 并且没有添加新行。

【讨论】:

    猜你喜欢
    • 2016-08-22
    • 2015-09-05
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多