0

In this code when I search in the book 0 index it shows the error ..... but the statements in elif statement work perfectly. When I manually enter the value instead of putting sea variable in my query for book[0] it gives me output, But I don’t know whats the problem with variable “sea”, its not working. please solve this problem. Thank you.

@app.route("/search",methods=["POST","GET"])
def search1():
    var=request.form.get("search2")
    sea=str(var)
    books=db.execute("select isbn,title,author,year from books").fetchall()
    for book in books:
        save="this"
        if sea in str(book[0]):
            save=db.execute("select isbn,title,author,year from books whereisbn="+sea).fetchall()
            return render_template("done.html",save=save)
        elif sea in str(book[3]):
            save=db.execute("select isbn,title,author,year from books whereyear="+sea).fetchall()
            return render_template("done.html",save=save)
2
  • Assuming isbn is a text type like VARCHAR, the +sea needs to be enclosed in '. Message means program is trying to compare varchar to integer Apr 21 '20 at 15:26
  • @DinoCoderSaurus But i have converted the 'var' variable into string and stored it in 'sea' variable, then why is the query reading it as an integer??
    – stormcloak
    Apr 23 '20 at 9:05
1

You know it's a string and python knows it's a string, but postgres doesn't. When the column is text type (eg VARCHAR), the proper syntax is:

SELECT col1, col2, col3 .... from table_name where col = 'text'

The text argument must be enclosed in single quotes ('). Something like

"SELECT......where isbn ='"+sea+"'"

There should be a space between where and isbn/year for readability. (Frankly I'm surprised the year query works at all without the space, but personal experience with postgres is very limited).

1
  • Thank you very much, it worked!!. After soo many days this issue is solved. Thank you again.
    – stormcloak
    Apr 24 '20 at 5:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .