I tried to create a single insert query for the values in template_list array. These values are for field template_name.
dir_template_list = ['template1', 'template2', 'template3']
sql = "INSERT INTO tbltemplate (template_name) VALUES(%s)" % (dir_template_list)
Is there a simple/easier way to create this query using string placeholder?
INSERT INTO tbltemplate (template_name) VALUES ('template1'),('template2'),('template3')
Edit: This is my actual table structure
FIELD
-------
id int(6)
template_name VARCHAR(128)
First of all, NEVER use string formatting to create queries – or you’ll risk SQL injection vulnerabilities, have to deal with escaping, etc.
The proper way is to use single queries for every row and passing the data as parameters:
To improve the performance you might want to do this in a transaction or using a prepared statement.