I am using 'insert...on duplicate key' as query for executemany, but it gives me error - "not all arguments converted during string formatting".
I have 5 columns in the table(col1, col2, col3, col4, col5).
col1 and col5 are the keys.
In case, a duplicate entry is found, I'd update col2, col3, col4. My query:
q = insert into table values (%s, %s, %s, %s, %s) on duplicate key update col2 = %s, col3 = %s, col4 = %s
tuple = (val1, val2, val3, val4, val5, val2, val3, val4)
#tuple above is a list. tuple = [(val1, val2, val3, val4, val5, val2, val3, val4),
(val1, val2, val3, val4, val5, val2, val3, val4),
(val1, val2, val3, val4, val5, val2, val3, val4)....]
cursor.executemany(q, tuple)
Upon printing the tuple, I get the following result(a list of tuples, as expected) :
[(1029, 4, 550.0, None, 199, 4, 550.0, None)....]
SOLUTION TO THE PROBLEM - q = insert into table (col1, col2, col3, col4, col5)values (%s, %s, %s, %s, %s) on duplicate key update col2 = values(col2), col3 = values(col3), col4 = values(col4)
No need to pass extra parameters, update values could be taken from the values passed on to the first part of the query itself.
I got that from here - MySQLdb returns not all arguments converted with "on duplicate key update"
"%s" % (1,2)- string has only one%sbut tuple has two values(1,2)%sso every tuple should have 8 elements. I would add code which checklen()for every tuple to test it. Or I would try to run query with every tuple sparatelly (and print tuple before use whith query) to catch which tuple makes problem.q = insert into table (col1, col2, col3, col4, col5)values (%s, %s, %s, %s, %s) on duplicate key update col2 = values(col2), col3 = values(col3), col4 = values(col4)