How To Use Wild Cards In Sqlalchemy?
I'm trying to use wildcards for a query using SQLAlchemy but I'm getting back an empty list. My code: engine = create_engine(os.getenv('DATABASE_URL')) db = scoped_session(sessionm
Solution 1:
The %
characters should be part of the parameter you pass in, not the template string, and you shouldn't be manually adding quotes. Let SQLAlchemy do that for you.
Also, there's no need for the template to be an f-string.
For example:
s = input("Search for a book: ")
q = db.execute(
"SELECT * FROM books WHERE isbn LIKE :s OR author LIKE :s OR title LIKE :s",
{"s": "%" + s + "%"},
).fetchall()
Post a Comment for "How To Use Wild Cards In Sqlalchemy?"