How do I login to the database?

Asked

Viewed 137 times

0

I am creating a study application for a simple login screen, on the same black screen. I created 2 files, one where I create the database query and the other with the application code itself. It has two tasks, login and registration. The log is working perfectly. However I could not come up with the logic for authentication. I made a select, had it printed to test and it’s returning right.

query sql

def select_db(self, username, password):
        self.cursor.execute('SELECT * FROM tbl_login WHERE username = ? AND password = ?',
                            (username, password))

application code

def login():
    db = Database('login.db')
    print('\n  ** ENTER WITH YOUR USERNAME **')
    username = input('  Username: ')
    password = getpass.getpass('  Password: ')
    auth = db.select_db(username, password)
    if username and password in auth:
        print('\n  Welcome to system!')
    else:
        print('\n  Login error! Try again!')
        login()

1 answer

0


You can use the method fetchone the cursor to bring the first query result, it will be the line with the user data or None if you don’t find:

def select_db(self, username, password):
    return self.cursor.execute(...).fetchone()

After that you need to adjust your if:

user = db.select_db(username, password)
if user:
     print('\n  Welcome to system!')
  • It was in the can huh! I feel bad for my stupidity, rs. Thanks, solved!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.