Menu

#209 executemany doesn't work with INSERT ... ON DUPLICATE KEY ..

MySQLdb-1.2
closed
MySQLdb (285)
5
2012-09-19
2006-09-11
Alexey
No

MySQLdb treats rest of the string after (%s,%s...%s)
as values list, including following ON DUPLICATE KEY
UPDATE statement.

Discussion

  • Andy Dustman

    Andy Dustman - 2006-09-11

    Logged In: YES
    user_id=71372

    No, I suppose it wouldn't. You ought to be able to use
    REPLACE as a workaround.

     
  • Alexey

    Alexey - 2006-09-12

    Logged In: YES
    user_id=1079993

    Unfortunately REPLACE doesn't do what i need.
    This code (shortened) from cursor.py (189):

    m = insert_values.search(query)
    p = m.start(1)
    qv = query[p:]
    qargs = db.literal(args)
    q = [ query % qargs[0] ]
    q.extend([ qv % a for a in qargs[1:] ])

    duplicates "(%s,%s) ON DUPLICATE KEY..." for each inserted
    record. Which is an SQL syntax error.

     
  • Andy Dustman

    Andy Dustman - 2006-09-12

    Logged In: YES
    user_id=71372

    Yeah, I get that.

    Change this line:

    insert_values = re.compile(r'\svalues\s*((.+))',
    re.IGNORECASE)

    to:

    insert_values = re.compile(r'\svalues\s((.+))(.)',
    re.IGNORECASE)

    After:

        p = m.start(1)
    

    add:

        tail = m.group(2)
    

    After:

            q.extend([ qv % a for a in qargs[1:] ])
    

    add:

            q.append(tail)
    

    See if that fixes it.

     
  • Alexey

    Alexey - 2006-09-12

    Logged In: YES
    user_id=1079993

    Thanks! I'll try.

     
  • Andy Dustman

    Andy Dustman - 2007-03-04

    Logged In: YES
    user_id=71372
    Originator: NO

    Believed to be fixed in 1.2.2

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.