tripkane
tripkane

Reputation: 47

SQL String throws error with special character in SQLIte column name

I have an SQLite database. In said table there exists a column that has a "-" / minus sign in it. This SQLite database was created from python 3.6 with pandas (SQLAlchemy as the engine). The table and this column is created without problem. However when I want to build a query on this table I don't know how to escape the "-" character. Here is a short example:

#imports
import numpy as np
import pandas as pd
from sqlalchemy import create_engine

#create df
df = pd.DataFrame(np.random.rand(10,2),columns=['Column1','Prob-Column'])
# create engine to connect to db
engine = create_engine('sqlite://')
#create table in db
df.to_sql('my_table',engine,if_exists='replace')

# variables
vals = '(?)'
fil = ('key',)

# create sql string
sq = 'SELECT * FROM {t} WHERE {c1} IN {vals} GROUP BY {c2}'\
.format(t='my_table',c1='Column1',c2='Prob-Column',vals = vals)

#write query to pandas df
df = pd.read_sql_query(sq,engine,params=(fil))

the trace is as follows:

OperationalError: (sqlite3.OperationalError) no such column: Prob [SQL: 'SELECT * FROM my_table WHERE Column1 IN (?) GROUP BY Prob-Column'] [parameters: ('key',)] (Background on this error at: http://sqlalche.me/e/e3q8)    

Upvotes: 0

Views: 1535

Answers (1)

tripkane
tripkane

Reputation: 47

Here is the solution. The column name just needs double-quotes around it i.e. on the inside of the single quotes such that c2='"Prob-Column"'. Anyway hope this helps someone else.

#imports
import numpy as np
import pandas as pd
from sqlalchemy import create_engine

#create df
df = pd.DataFrame(np.random.rand(10,2),columns=['Column1','Prob-Column'])
# create engine to connect to db
engine = create_engine('sqlite://')
#create table in db
df.to_sql('my_table',engine,if_exists='replace')

# variables
vals = '(?)'
fil = ('key',)

# create sql string
sq = 'SELECT * FROM {t} WHERE {c1} IN {vals} GROUP BY {c2}'\
.format(t='my_table',c1='Column1',c2='"Prob-Column"',vals = vals)

#write query to pandas df
df = pd.read_sql_query(sq,engine,params=(fil))

Upvotes: 1

Related Questions