I am given a Python List of an arbitrary length and containing arbitrary strings. In particular, it can have strings with embedded single and/or double quotes. I have no control over the input so I have to take what I am given.
For example:
valueList = [ "hello'world", 'foo"bar', 'my\'name"is', "see\'you\"soon" ]
Python shell:
>>> valueList = [ "hello'world", 'foo"bar', 'my\'name"is', "see\'you\"soon" ]
>>>
>>> valueList
["hello'world", 'foo"bar', 'my\'name"is', 'see\'you"soon']
>>>
>>> valueList[0]
"hello'world"
>>>
>>> valueList[1]
'foo"bar'
>>>
>>> valueList[2]
'my\'name"is'
>>>
>>> valueList[3]
'see\'you"soon'
From this, I need to generate an SQL string such as:
"SELECT * FROM myTable as mt
WHERE mt."colName" IN ("hello'world", 'foo"bar', 'my\'name"is', 'see\'you"soon')
Any solution has to work with both SQLite and Postgres.
I have tried to generate the (...) portion of the clause using Python join but that just ends up making one big string with all single quotes escaped. For example:
Python shell:
>>> values = "','".join(valueList)
>>> values
'hello\'world\',\'foo"bar\',\'my\'name"is\',\'see\'you"soon'
>>> values = "'" + "','".join(valueList) + "'"
>>> values
'\'hello\'world\',\'foo"bar\',\'my\'name"is\',\'see\'you"soon\''
Additional info: The code that I inherited uses SQLAlchemy and Pandas.
import pandas as pd
...cut...cut...cut...
my_df = pd.read_sql(sql, my_conn);
I do NOT want to use Pandas to do the filtering. In fact, my assigned task is to REMOVE the existing Pandas filtering and replace it with SQL with explicit WHERE/IN filters for speed.
For example, replace this:
my_df = pd.read_sql("SELECT * FROM myTable", my_conn) <==== can return 10's of thousands of rows
my_df = my_df[my_df.loc[:, 'colName'].isin(myList)] <==== ends up with a handful of rows
with this:
my_df = pd.read_sql("SELECT * FROM myTable as mt WHERE mt."colName" IN ("hello'world", 'foo"bar', ...)", my_conn)
SQL injection protection is a plus, but at this point I'll be happy with any solution that works.
colNamea column ofmyTable? Is it that you want to retreive the rows that at columncolNamehas one of values included in you original list?pd.read_sql(sqlStmnt, conn). But, first I want to generate the potentially very long WHERE/IN clause. Yes, colName is a column of myTable (not the real name, of course). Just as an example. I want this:"SELECT * FROM myTable as mt WHERE mt."colName" IN ("hello'world", 'foo"bar', 'my\'name"is', 'see\'you"soon')