Using MySQL python module to Insert or update if row exists -


i using mysql , python mysqldb module. want insert new row or update fields if row exists.

the list of tuples contains values entered looks this;

listtuple=[('a_val', 'b_val', 'c_val', 'd_val', 'c_val'), ('aa_val', 'bb_val', 'cc_val', 'dd_val', 'cc_val')] 

the mysql query looks this;

query="""insert table (a, b, c, d)              values (%s, %s, %s, %s)                on duplicate key update              c= %s       """  try:     cursor.executemany(query, listtuple)       db_conn.commit() except:     db_conn.rollback()  

query execution fails code. can point out wrong it? thank help.

try this:

query="""insert table (a, b, c, d)              values (%s, %s, %s, %s)                on duplicate key update              c= values(c)       """ 

your query had 5 placeholders, tuples have 4 values. needed duplicate c, values(c) option automatically original values clause.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -