Pandas read_sql_query with WHERE condition on VARCHAR

557 Views Asked by At

I am trying to pull data from a certain database, the database is huge so I would like to query it before getting the data into python. I want to query on a specific column [Type] that I know is type varchar(50). As a test tried to query on an integer column successfully:

df = pd.read_sql_query('SELECT * FROM Prices where Price = 1', conn)

However when I try and do the same with the varchar column it tells me the column name is invalid.

df = pd.read_sql_query('SELECT * FROM Prices where Type = Daily', conn)

Thanks in advance!

1

There are 1 best solutions below

0
Luca Esposito On

I incurred to the same problem, I solved using a combination of strings (especially the docstring). You should try something like:

df = pd.read_sql_query('''SELECT * FROM "Prices" where "Type" = 'Daily'
''', conn)

The line brake is important! I think that for the sake of sql syntax, the quote on the value must be of this type: ' and not ".