I need to pass a dynamic list of strings into raw sql query. Here is my code:
myList = ['PREFIX\000\000923', 'PREFIX\000\000CS3'] # <- strings I have troubles with
myList = ['OK1', 'OK2'] # <- ok strings
myTuple = tuple(myList)
query = "SELECT * FROM public.items WHERE name IN {}".format(myTuple)
result = cursor.execute(query, myTuple)
rows = dict_fetch_all(cursor)
for row in rows:
print(row)
The above piece of code works just fine. However, there is a problem with strings with special characters with backslashes like this: "PREFIX\000\000923".
What is the right way to code it?
[EDIT] Here is the printed query in the console:
SELECT * FROM public.items WHERE name IN ('PREFIX\x00\x00923', 'PREFIX\x00\x00CS3')
As you see, myList items have been converted to some strange string.