I am looking for a way to drop values from a list into a prepared SQL string which has question marks as placeholders. I have used this before in PyQT, but there I use the bindValue function. Using pymysql there looks to be no such function.
Here's an example of the type of SQL string I have prepared:
INSERT INTO my_table (`Column1Name`, `Column2Name`, `Column3Name`) VALUES (?,?,?);
I then have a list of values I am looking to insert into (or link to) the question mark placeholders.
my_values_list['string_1', '3', 'anothervalue']
Like I say, I have used this method before in PyQT, so I know this '?' placeholder method works, but without the bindValue function I can't see how to get it to work using pymysql.
Here's how I got it to work using PyQT's QSqlQuery bindValues function if it helps, where query.exec_() executes the SQL string:
if my_values_list:
[self.query.bindValue(i, my_values_list[i]) for i in range(len(my_values_list))]
self.query.exec_()
Here is an example of how to do this with PyMySQL:
query = 'INSERT INTO my_table (Column1Name, Column2Name, Column3Name) VALUES (%s, %s, %s);'
cur.execute(query, ('string_1', '3', 'anothervalue', ))
?
is not a valid placeholder here, but %s
is valid.