Python Forum
Issues with the task of reseaching a user in a Flask datbase
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues with the task of reseaching a user in a Flask datbase
#1
Hello, I’m a beginner with SQL and Flask and i’m having issues with a part of my website.
My goal is that a user fills an HTML form in which they fill in a value (in my case it's "name").
And with that information, the program has to check if this "name" is in the database:

* if it's in the database, you go to a webpage that prints out the other information associated with "name
(for example it prints out : "name","email","address")

* if it's not it sends you to a webpage that tells the user that the "name" doesn't exist in the database. (or "error" in my case)



My issue is that in this part of the code, it always sends the webpage where it indicates an "error"(line 13) even though I've inputed the correct information for it to normally work.

And when I remove the "except" command, I get this message after filling in the form:
UnboundLocalError: local variable 'tuple' referenced before assignment



* Here is an exctract of the python code:
@app.route("/searchrecord",methods = ["GET","POST"])
def searchrecord():
    name = request.form["name"]
    with sqlite3.connect("Student.db") as con:
        try:
            cur = con.cursor()
            cur.execute("SELECT name, email, address FROM Student")
            records = cursor.fetchall()
            for i in records:
                if i[0] == name:
                    tuple = i
        except:
            tuple = 'Error'
        finally:
            return render_template("search_record.html",tuple = tuple)
* And the HTML code for "searchrecord":
<!DOCTYPE html>  
 <html>  
 <head>  
     <title>search record</title>  
 </head>  
 <body>  

 <h3>{{tuple}}</h3>  
 <a href="/view">View List</a>  
 </body>  
 </html> 
So I'd like try to try to fix the issue where it seems that it's unable to extract and print out the result of the "try" (line 5) block.

Attached Files

.zip   ProjetNSI - Copy - Copy.zip (Size: 5.34 KB / Downloads: 312)
Reply
#2
Hi @Tsk42,

There are a number of things to say.
  1. NEVER use "tuple" as a name for a variable. Because it hides the builtin tuple.
  2. try - except blocks are great but they hide errors occurring. So if you have a problem like this, first remove the try - except to see the complete error message.
  3. When you remove "except" you get "local variable 'tuple' referenced before assignment". So apparently line 11 (tuple = i) is not executed.
  4. Always show the complete error trace you get. Not only the one line you are showing. The complete error message will show exactly on which line the error occurred. So tell us.
  5. On line 6 you assign the name "cur" for cursor. But on line 8 you use "cursor.fetchall()".
  6. You are loading the whole table with "cur.fetchall()" but imagine in real-life the table may contain millions of names. Then you would put a strain on your database producing millons of tuples and again on your program to check all those tuples. You had better have the database do the selection. That is what databases are made for.
    cur.execute("SELECT name, email, address FROM Student where name = ?", (name,))
    (Consult the documentation. You may need to use "%s" instead of "?".)

Please show us your next version and tell us if it works.
Reply
#3
(Nov-04-2021, 10:39 AM)ibreeden Wrote: NEVER use "tuple" as a name for a variable. Because it hides the builtin tuple.

It's also just a poor name, because it doesn't convey any meaning. What is the variable for? What is its significance in your program? Having meaningful names for things is important because it helps the code to be readable.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020