Skip to content

New executemany() implementation supporting UPDATE query #506

Description

@methane

SQLAlchemy supports update batching using Cursor.executemany().
Sadly, executemany in PyMySQL (and mysqlclient) only support INSERT and REPLACE
query.

I'll add new executemany implementation which uses multi results.
While it doesn't reduce number of queries, it can reduce number of roundtrips.

Activity

  1. added this to the milestone on Aug 30, 2016
  2. self-assigned this
    on Aug 30, 2016
  3. removed this from the milestone on Dec 20, 2017
  4. paytmMatt commented on Jun 14, 2018

    @paytmMatt

    The problem is with the regular expression RE_INSERT_VALUES in cursors.py

    You can see in this screenshot that not all the %s are captured by the regular expression and you get this error "TypeError: not all arguments converted during string formatting" on line 213 " v = values % escape(next(args), conn)

    image

  5. methane commented on Jun 14, 2018

    @methane
    MemberAuthor

    I never want add real SQL parser. So I never support such query by regular expression.
    executemany() by multi query will solve it.
    But I don't have motivation for improving this project now.

  6. methane commented on Jun 14, 2018

    @methane
    MemberAuthor

    MariaDB supports original bulk insert.
    https://mariadb.com/kb/en/library/bulk-insert-row-wise-binding/

    If MySQL follow MariaDB, I want to implement it, instead of multi statement hack.

  7. OmniTroid commented on Mar 10, 2020

    @OmniTroid

    +1 would like to see this implemented as well.

  8. Caozhitong commented on Jul 18, 2020

    @Caozhitong

    +1

  9. langshi2555 commented on Jul 23, 2020

    @langshi2555

    +1

  10. piweidong commented on Sep 11, 2021

    @piweidong

    +1 would like to see this implemented as soon as possible.

  11. realcp1018 commented on Sep 11, 2024

    @realcp1018

    It seems that execute() works well for code like cursor.execute("insert into t(c1, c2) values(%s, %s) on duplicate key update c1=%s, c2=%s", (1, 2)*2), all the 4 args will be assigned.
    But executemany() for insert into ... on duplicate key update failed because RE_INSERT_VALUES can not match placeholders after ON DUPLICATE, there're 2 solutions:

    • use execute instead of executemany for insert into ... on duplicate key update
    • use mysql VALUES() functions after update to refer values for insertion, like cursor.executemany("insert into t(c1, c2) values(%s, %s) on duplicate key update c1=Values(c1), c2=Values(c2)", [(1, 2), (3, 4)])
  12. added this to the v1.3 milestone on Oct 1, 2026
  13. added theissue type on Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions