Python Forum
Problem applying conditions against results back from a database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem applying conditions against results back from a database
#21
anyone who can help?

I don't need riddles - i need help. Thanks!
Reply
#22
(Jun-12-2020, 11:54 PM)card51shor Wrote: anyone who can help?

I don't need riddles - i need help. Thanks!

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
Reply
#23
i'm not sure what he said that i haven't answered
Reply
#24
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
Reply
#25
(Jun-12-2020, 11:54 PM)card51shor Wrote: anyone who can help?

I don't need riddles - i need help. Thanks!

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
Reply
#26
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 :

Error:
TypeError TypeError: 'NoneType' object is not subscriptable Traceback (most recent call last) File "C:\Python38\Lib\site-packages\flask\app.py", line 2464, in __call__ return self.wsgi_app(environ, start_response) File "C:\Python38\Lib\site-packages\flask\app.py", line 2450, in wsgi_app response = self.handle_exception(e) File "C:\Python38\Lib\site-packages\flask\app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb) File "C:\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise raise value File "C:\Python38\Lib\site-packages\flask\app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "C:\Python38\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Python38\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise raise value File "C:\Python38\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "C:\Python38\Lib\site-packages\flask\app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "C:\school\project1\application.py", line 42, in login if username is not None and usernamedata[0] == username and passworddata[0] == password: TypeError: 'NoneType' object is not subscriptable
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")
Reply
#27
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]
Error:
TypeError: 'NoneType' object is not subscriptable
Reply
#28
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.
Reply
#29
usernamedata = None

if not usernamedata:
    print('invalid username')
Output:
invalid username
Reply
#30
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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  applying total in a loop to a list JustinxFFx 1 2,189 Feb-11-2018, 03:14 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

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