I have an example SQL query string like this:
query = "select * from myTbl where name in ('apple', 'pear')"
I need to replace ('apple', 'pear')
with any python list generated.
How I can insert any list into the SQL query string without hardcoded in. The code below does not work:
myList = ['apple', 'pear']
sList = ','.join(myList)
"select * from myTbl where name in ({})".format(sList)
It gives a query string 'select * from myTbl where name in (apple,pear)'
What I need is a query string "select * from myTbl where name in ('apple','pear')"
您缺少每个元素周围的引号:
sList = ','.join('\'' + i + '\'' for i in myList)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.