I am trying to run some database queries with SQLAlchemy
. But I am having an issue querying for strings that have apostrophes or any kind of non-alphanumeric character.
For example: test = "I\'ve"
I used:
matchList = session.query(Quote).filter(Quote.quote.contains(test)).all()
There is 1 record in the database that contains "I've"
as a substring. But, matchList is turning out to be an empty list. What am I doing wrong?
Full Code:
sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_declarative import Base, Quote
from sqlalchemy_utils import escape_like
engine = create_engine("sqlite:///quotes.db")
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
test = "I\'ve"
qList = session.query(Quote).filter(Quote.quote.contains(test.replace('\'', ''))).all()
print qList
It seems like SQLAlchemy
doesn't handle escape sequence. Could anyone help