user12426459
user12426459

Reputation:

How do I compare an input to data that's already in an SQLite3 table?

I want to compare a user input with data that's already in a table?

So far I've done the following code

db_info = cursor.execute("SELECT * FROM usersTable");
    if login_user in db_info:
        game()
    else:
        messagebox.showerror("Invalid Details", "Inputted details are invalid!")

When I run this, nothing happens so I'm not sure where I've gone wrong. Anyone got any suggestions?

Upvotes: 0

Views: 550

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 149185

The common way to find a username in a database is to use a where clause:

db_info = cursor.execute("SELECT * FROM usersTable WHERE username=?",
                         [login_user]).fetchone()

if db_info is not None:
    # check password
    game()
else:
    messagebox.showerror("Invalid Details", "Inputted details are invalid!")

Upvotes: 1

DinoCoderSaurus
DinoCoderSaurus

Reputation: 6520

db_info is a cursor object therefore this login_user in db_info will always evaluate to false.

From the python sqlite 3 API interface doc:

To retrieve data after executing a SELECT statement, you can either treat the cursor as an iterator, call the cursor’s fetchone() method to retrieve a single matching row, or call fetchall() to get a list of the matching rows.

There are plenty of examples in the doc that will inform you how to get the data so you can construct the right logical test.

Upvotes: 0

Related Questions