python - Reuse %s placeholder without duplicating the variable? -


mysql ver 14.14 distrib 5.7.19, linux (x86_64)

i'm running insert / on duplicate key update query this:

query = ("insert table set col1 = %s, col2 = %s, col3 = %s on duplicate key update col2 = %s, col3 = %s") cursor.execute(insert_post, (var1, var2, var3, var2, var3,)) 

is there way without reusing each of variables in cursor.execute() function, or better off doing string formatting format()?

iiuc, use named formatters , pass dict second argument.

query = ("""insert table              set col1 = %(var1)s,                  col2 = %(var2)s,                  col3 = %(var3)s              on duplicate key update col2 = %(var2)s,                                      col3 = %(var3)s""") cursor.execute(insert_post, {'var1' : var1, 'var2' : var2, 'var3' : var3}) 

reference.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -