python execute many with “on duplicate key update”?

后端 未结 4 1928
臣服心动
臣服心动 2021-01-06 15:28

I am trying to executemany in python with on duplicate key update, with the following script:

# data from a previous query (returns 4 integers in each row)
r         


        
4条回答
  •  梦谈多话
    2021-01-06 15:49

    When you write sql like following:

    sql = insert into A (id, last_date, count) values(%s, %s, %s) on duplicate key update last_date=%s, count=count+%s'
    

    You will get the following error: TypeError: not all arguments converted during string formatting.

    So when you use "ON DUPLICATE KEY UPDATE" in python, you need to write sql like this:

    sql = 'insert into A (id, last_date, count) values(%s, %s, %s) on duplicate key update last_date=values(last_date),count=count+values(count)' 
    

提交回复
热议问题