4
\$\begingroup\$

I'm a new programmer who is currently working on a personal project. It is supposed to be a login program which stores user data in a SQLite database.

I was wondering if anyone could look at my code and provide any constructive criticism. Please contribute, regardless of how insignificant the flaw may seem.

Anyway without further ado, here is my code:

import sqlite3, time


def is_valid_username(username):

    find_user = ("SELECT * FROM user WHERE username = ?")
    cursor.execute(find_user,[(username)])
    results = cursor.fetchall()             
    return results

def is_valid_password(username, password):


    find_user = ("SELECT * FROM user WHERE username = ? AND password = ?")
    cursor.execute(find_user, [(username), (password)])
    result = cursor.fetchall()
    return result

def create_new_user():
    while True:
        username = input("Enter a new username: ")
        find_user = ("SELECT * FROM user WHERE username = ?")
        cursor.execute(find_user, [(username)])

        if (cursor.fetchall()):
            print("Username taken, please try again")
        else:
            break
    firstName = input("Enter your first name: ")
    surname = input("Enter your surname: ")
    password = input("Enter your password: ")
    repassword = input("Reenter your password: ")
    while password != repassword:
        print("Passwords do not match, try again.")
        password = input("Enter your password: ")
        repassword = input("Reenter your password: ")
    insert = "INSERT INTO user(username, firstname, surname, password) VALUES(?,?,?,?)"
    cursor.execute(insert, [(username), (firstName), (surname), (password)])
    db.commit()
    print("Account created")
    time.sleep(2)
    return

with sqlite3.connect("Users.db") as db:
    cursor = db.cursor()

    cursor.execute('''
    CREATE TABLE IF NOT EXISTS user(
    userID INTEGER PRIMARY KEY,
    username VARCHAR(20) NOT NULL,
    firstname VARCHAR(20) NOT NULL,
    surname VARCHAR(20) NOT NULL,
    password VARCHAR(20) NOT NULL);
    ''')
   
for i in range(3):
    username = input("Please input username: ")
    if (is_valid_username(username)):
        password = input("Please input password: ")
        if is_valid_password(username, password):
            print('Username and password accepted, welcome.')
            time.sleep(3)
            exit()
        else:
            print("Incorrect password, please try again?: ")
    else:
        Q1 = input("Not valid user. Would you like to create an account?: ")
        reply_yes = ('y', 'yes')
        if Q1.lower() in reply_yes:
            create_new_user()'
\$\endgroup\$

1 Answer 1

-2
\$\begingroup\$

What about connecting to your database?....creating the cursor? I think those are necessary.

conn = sqlite3.connect("database.db")
c = conn.cursor()
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Are you suggesting that didn't happen in the OP code? Right after with sqlite3.connect("Users.db") as db: we see a call to .cursor() \$\endgroup\$
    – J_H
    Commented Sep 5, 2023 at 4:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.