1

I am trying to check for a username in a data base, when i do it works for the 1st username but after it doesnt work, i think i understand why but i cant work out an alternative. Here is my code:

import sqlite3
conn = sqlite3.connect('tutorial.db')
c = conn.cursor()
def username():
    global limit
    global usernameinput
    usernameinput = input("What would you like your username to be?")
    limit = 0
    select_all_tasks(conn)
def create_table():
    c.execute('CREATE TABLE IF NOT EXISTS stuffToPlot(username TEXT, 
    password TEXT)')
    conn.close
def select_all_tasks(conn):
    global limit
    global passwordinput
    global rows
    c.execute("SELECT * FROM stuffToPlot")
    rows = c.fetchall()
    for row in rows:
        print(row)
    if usernameinput in row:
        print("Username Taken")
        username()
    else:
        if limit < 1:
            passwordinput = input("What would you like your password to 
            be?")
            limit = limit + 1
def data_entry():
    global passwordinput
    global row
    c.execute("INSERT INTO stuffToPlot VALUES(?, ?);",(usernameinput, 
    passwordinput))
    conn.commit()
    c.close()
    conn.close()


create_table()
username()
select_all_tasks(conn)
data_entry()

There is no error, it just doesn't register that the username is already in the database.

3
  • is if usernameinput in row: indented like this in your code ? Commented Oct 29, 2017 at 21:01
  • No, it is how its meant to be Commented Oct 29, 2017 at 21:04
  • I know this isn't the main point of your question so I'm adding it as a comment. Storing an unencrypted password in a database is a very bad approach. Minimally it should be encrypted with a salt. There are many resources you could draw on to devise that kind of solution. As usual with security-related issues, "roll your own" is a terrible idea. Commented Oct 30, 2017 at 0:27

1 Answer 1

2

You need to indent the if statement and all the following lines to make it work, otherwise only the last row is tested.

for row in rows:
    print(row)
    if usernameinput in row:
        print("Username Taken")

Also you could make it simpler :

# select only the field "username" from the table
c.execute("SELECT username FROM stuffToPlot") 
# build a set with all names, from the results given as [('name1',), ('name2',), ('name3',)]
names = {name[0] for name in c.fetchall()} 
if usernameinput in names:  
    print("Username Taken")
    username()
else:
    if limit < 1:
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried the 1st one and it still doesnt work, edit: However the 2nd does, thank you
what's the purpose of the limit exactly ? in fact, the first pattern is not correct, you should try the second one, it make only one test instead of repeating the if/else statements for each row of the loop
i had the limit because it wouldnt add the username until after the function was complete so it asked for the password twice and the second works so thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.