0

I have seen alot of threads to check if the field exists but i am a little confused i was wondering if someone could tell me hwot to check if it exists and to see if it is equal to an input which is a variable.

My code so far

import sqlite3
import datetime
import smtplib

now = datetime.datetime.now()
conn = sqlite3.connect("accounts.db")

c = conn.cursor()



def register():
    username = input("Username: ")
    password = input ("Password: ")
    date = now.strftime("%d-%m-%Y %H-%M")
    adminlevel = 0

    c.execute("""INSERT INTO accounts
              (username,password,date,adminlevel)
              VALUES(?,?,?,?)""",(username,password,date,adminlevel))
    conn.commit()

    print("You have succesfully registered at " + date, "An email has been sent to you with your information!")
    menu()

from getpass import getpass

def login():
    def loggedin():

        if adminlevel > 0:
            print("Hello There Admin!")
            print("Commands: ")
        else:
            print("Hello there {}".format(username))
            print("Commands: Name, Date, Friend, Logout")





    adminlevel = 0
    username = input("Please enter your username: ")
    password = getpass("Please enter your password: ")
    admin = c.execute("select 1 from accounts where adminlevel > 0")
    c.execute("select 1 from accounts where username = ? and password = ?", (username, password))
    if c.fetchone():
        print('Hello there! {}'.format(username))
        loggedin()
    else:

        print("Username and Password Not Found!")
        login()

def menu():
    print("="*40)
    print("Login & Registration System")
    print("="*40)

    choice = input("Login or Register? ")

    if choice == "register":
        register()
    elif choice == "login":
        login()


menu()
3
  • 1
    Do you mean in your login() function? You should make more effort to make your question clear - it should not be necessary to read all of the code to figure out what the question might be. Commented Apr 11, 2016 at 11:03
  • For starters, def info(): does nothing (global variables should be declared global in the function that uses them) and username == username: will always be true - if username was defined, that is. Commented Apr 11, 2016 at 11:13
  • Don't worry got rid of it now, i have my answers :) Commented Apr 11, 2016 at 11:24

1 Answer 1

1

Assuming that you want to check the user's credentials in the login() function, you can perform a SELECT query for that:

from getpass import getpass

def login():
    username = input("Please enter your username: ")
    password = getpass("Please enter your password: ")
    c.execute("select 1 from accounts where username = ? and password = ?", (username, password))
    if c.fetchone():
        print('logged in: {}'.format(username))
    else:
        print("ERROR")

This code prompts the user for their username and password (without echoing it to the terminal), and then uses those values to query the database for a matching record in the account table. If there is a record, 1 will be returned by fetchone() and the user can then be logged in, otherwise the username and/or password are incorrect.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! if i would like to define loggedin() and grab the username how would i do this? would i need to make username a global varible? then print it etc
You mean that loggedin() is a function that will log the user in? If so then simply pass the user name as an argument to that function, i.e. loggedin(username). You don't need to use global variables.
Ok, i just made it so when they log in it will call that definition / function then do a set of commands
Does not recognize it as its not in log in, i tried to put it in the same definition but it referenced before assignment.

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.