Home:ALL Converter>dynamic mysql insert statement in python

dynamic mysql insert statement in python

Ask Time:2017-09-30T11:04:45         Author:user3855343

Json Formatter

I need to update columns values in csv to mysql database and the csv values are dynamic for one file it may be 10 columns and for other it may be 5 columns.

my understanding in python we need to use list also this question raised earlier is similar to my requirement but here the values are static so it can be predefined where's in my case being dynamic need to a solution to have %%s under VALUES to be multiplied according to my column values dynamically.

MySQL Dynamic Query Statement in Python

   ds_list=['name','id','class','chapter','MTH','YEAR']

   vl_list=['xxxxx','978000048','x-grade','Science',mar,2017]

   sql = 'INSERT INTO ann1 (%s) VALUES (%%s, %%s, %%s, %%s, %%s, %%s)' %    ','.join(ds_list)

   cur.execute(sql, vl_list)

   conn.commit()

Author:user3855343,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/46499191/dynamic-mysql-insert-statement-in-python
yy