简体   繁体   中英

how to insert variable in SQL into LIKE query in flask?

I'm trying to make a search bar, which means that I have to make it to take incomplete data. I have this code, but for some reason it gives me a syntax error, when I try to print it into terminal, in order to look at it. Any ideas what could be wrong? I think I made a mistake when I was putting the variables in, but I can't really find anything about it on google.

Anyway, here is my code:

BookSearch = db.execute(text("SELECT * FROM books WHERE 
        (isbn LIKE :search) OR (title LIKE :search) OR (author LIKE :search)", 
        { "search": '%' + search + '%'}).fetchall()
print(BookSearch)

text() does not have a close parenthesis which is likely causing your syntax error.

I'm assuming it should go right after the " and the dictionary is passed to execute()

BookSearch = db.execute(text("SELECT * FROM books WHERE 
        (isbn LIKE :search) OR (title LIKE :search) OR (author LIKE :search)"), 
        {"search": '%' + search + '%'}).fetchall()
print(BookSearch)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM