SourOddity
SourOddity

Reputation: 141

Is there a way I can check a specific field in a record, using an existing python variable in SQLite

Sorry if this question seems convoluted or is easilt googlable, I was struggling to find anything particularly helpful. Essentially, at the beginning of my python program I have a login which checks the database to see if the username and password match:

existingAccount = input("Do you already have an account? (y/n)")
if existingAccount == "y":

    username = input("Please enter your username: ")
    password = input("Please enter your password:")

    find_user = ("SELECT * FROM Users WHERE Username = ? AND Password = ?")
    c.execute(find_user, [(username), (password)])
    results = c.fetchall()

This creates the variable results which I want to use as a way of checking the active user for creating an order and checking their permission level which is stored in the Users table.

I am going to write a function which will allow someone to add a new product to the products table but i only want users with the permission level admin to be able to do it.

Is there a way I can do some sort of if statement using the results variable to say "if results contains userPermissionLevel Admin then continue, if not print "permission level inadequate"

Thanks and sorry again if this is an easy question.

Upvotes: 2

Views: 105

Answers (1)

IoaTzimas
IoaTzimas

Reputation: 10624

Considering that results will have only one row (which is the normal) you can just check the results[0] item and the respective position of your userPermissionLevel column. For example if it's your 5th column you can just check with the following:

if results[0][5]=='Admin':
    do_some_stuff
else:
    print('permission level inadequate')

You can also check the specific column by name, if you have run this line after the creation of your sqlite3 connection:

conn.row_factory = sqlite3.Row

In that case, you can change the aboce code to this, using the column name:

if results[0]['userPermissionLevel']=='Admin':
    do_some_stuff
else:
    print('permission level inadequate')

Upvotes: 1

Related Questions