Jun-12-2020, 11:54 PM
anyone who can help?
I don't need riddles - i need help. Thanks!
I don't need riddles - i need help. Thanks!
Problem applying conditions against results back from a database
|
Jun-12-2020, 11:54 PM
anyone who can help?
I don't need riddles - i need help. Thanks!
Jun-13-2020, 05:12 AM
(This post was last modified: Jun-13-2020, 05:13 AM by pyzyx3qwerty.)
(Jun-12-2020, 11:54 PM)card51shor Wrote: anyone who can help? In the homework section, we don't post answers, we only help. And I think what @ndc85430 has walked you through the most help we can do in this section
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela Need help on the forum? Visit help @ python forum For learning more and more about python, visit Python docs
Jun-13-2020, 05:21 AM
i'm not sure what he said that i haven't answered
Jun-13-2020, 07:56 AM
Having answered and having understood and implemented in your code are two very different things, @card51shor
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela Need help on the forum? Visit help @ python forum For learning more and more about python, visit Python docs (Jun-12-2020, 11:54 PM)card51shor Wrote: anyone who can help? This thread is getting a bit out of hand, reading through all the post its hard to tell what the actual problem is any more. Just going from the first post, what's stored in the database does not match when doing a comparison that you expect to match? Is the data on the database wrong? If so are you adding the data to the database? and if that is the case you would need to show how you are adding the data. If you are not in control of the data and you just need to work with what you get back: Is the data consistent, could you alter the format of the string you are comparing to match the format of the database data? If you want to remove the unnecessary extra characters and don't know how you would need to show examples of what the data looks like and how you would like it to be. In summary, the code shown is not runnable by anyone else but yourself, so you would either need to: show something runnable to enable people trying to help to see the problem or give examples of the strings you are comparing. This thread either needs bringing back on track with an update of what you actually need and draw a line under the rest or closing and a new thread started cleary giving the problem at hand. Please help the forum to help you
Jun-13-2020, 04:36 PM
(This post was last modified: Jun-13-2020, 04:37 PM by card51shor.)
OK well I am entering the data. I have a registration code here :
@app.route("/register", methods=["GET", "POST"]) def register(): if request.method == "GET": return render_template("register.html") elif request.method == "POST": password = request.form.get("password") username = request.form.get("username") session.clear() db.execute("INSERT INTO users (username, password) VALUES (:username, :password)", {"username":username, "password":password}) db.commit() return render_template("login.html")the issue with my app is: 1) When I login with the incorrect username and password - i get an error. 2) When I login with a correct username and incorrect password - it works. 3) When I login with correct username and correct password - it works. It only doesn't work on the first option when both are wrong - it gives me an error like this : Here's my code in question: @app.route("/login", methods=["GET","POST"]) def login(): password = request.form.get("password") username = request.form.get("username") usernamedata = db.execute("SELECT username FROM users WHERE username=:username", {"username":username}).fetchone() passworddata = db.execute("SELECT password FROM users WHERE username=:username", {"username":username}).fetchone() if username is not None and usernamedata[0] == username and passworddata[0] == password: print("welcome") return render_template("welcome.html") elif username is not None and (usernamedata[0] != username or passworddata[0] != password): print("no match") return render_template("login.html") else: print("empty") return render_template("login.html")
Jun-13-2020, 05:00 PM
In the case of When I login with the incorrect username and password - I get an error.
what are the returned values from usernamedata = db.execute("SELECT username FROM users WHERE username=:username", {"username":username}).fetchone() passworddata = db.execute("SELECT password FROM users WHERE username=:username", {"username":username}).fetchone()and what do you expect them to be. What is db, what database package does it refer to, what does the documentation state it will return from the method fetchone when there are no records. If it returns None you will get the error usernamedata = None usernamedata[0]
Jun-13-2020, 05:13 PM
oooo Thanks. OK so that makes sense. It's giving an error when it's trying to get the index [0] of a None value.
I still don't quite see how I fix it, though.
Jun-13-2020, 05:19 PM
usernamedata = None if not usernamedata: print('invalid username')
Jun-13-2020, 05:22 PM
Hmm i tried that code and i'm still getting the same error. Where do I put it? Here's what I have:
@app.route("/login", methods=["GET","POST"]) def login(): password = request.form.get("password") username = request.form.get("username") usernamedata = None if not usernamedata: print('invalid username') usernamedata = db.execute("SELECT username FROM users WHERE username=:username", {"username":username}).fetchone() passworddata = db.execute("SELECT password FROM users WHERE username=:username", {"username":username}).fetchone() if username is not None and usernamedata[0] == username and passworddata[0] == password: print("welcome") return render_template("welcome.html") elif username is not None and (usernamedata[0] != username or passworddata[0] != password): print("no match") return render_template("login.html") else: print("empty") return render_template("login.html") |
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
applying total in a loop to a list | JustinxFFx | 1 | 2,218 |
Feb-11-2018, 03:14 AM Last Post: nilamo |