|
| 1 | +import mysql.connector |
| 2 | + |
| 3 | +database_connection = False |
| 4 | +running = True |
| 5 | +pass_weak = False |
| 6 | +login_status = False |
| 7 | + |
| 8 | +def database_connect(host_v,usr,password,datab): |
| 9 | + global database_connection, db, mycursor |
| 10 | + try: |
| 11 | + db = mysql.connector.connect( |
| 12 | + host=host_v, |
| 13 | + user=usr, |
| 14 | + passwd=password, |
| 15 | + database=datab |
| 16 | + ) |
| 17 | + mycursor = db.cursor() |
| 18 | + if db: |
| 19 | + database_connection = True |
| 20 | + return "Connection to the database is working" |
| 21 | + except: |
| 22 | + return "Connection to the database wasn't successful" |
| 23 | + |
| 24 | +def setup_database_connection(): |
| 25 | + global database_connection |
| 26 | + host_v = input("Input the hostname (Type localhost if you're not sure): ") |
| 27 | + usr = input("Input the database user: ") |
| 28 | + password = input("Input the database password: ") |
| 29 | + datab = input("Input the database name: ") |
| 30 | + print(database_connect(host_v,usr,password,datab)) |
| 31 | + if database_connection == False: |
| 32 | + exit() |
| 33 | + |
| 34 | +print("Welcome to Login System v1.2!") |
| 35 | +print("") |
| 36 | +while running: |
| 37 | + if not database_connection: |
| 38 | + setup_database_connection() |
| 39 | + print("") |
| 40 | + print("1) Create a table (Mandatory before registering or logging in): ") |
| 41 | + print("2) Register") |
| 42 | + print("3) Login") |
| 43 | + print("4) Quit") |
| 44 | + print("") |
| 45 | + user_input = input("Input a number: ") |
| 46 | + if user_input == str(4): |
| 47 | + print("") |
| 48 | + print("Thank you for using our program!") |
| 49 | + exit() |
| 50 | + elif user_input == str(1): |
| 51 | + try: |
| 52 | + mycursor.execute("CREATE TABLE system_data (name VARCHAR(20), password VARCHAR(20), personID int PRIMARY KEY AUTO_INCREMENT)") |
| 53 | + except: |
| 54 | + print("Table already created or something else went wrong!") |
| 55 | + elif user_input == str(2): |
| 56 | + register_name = input("Input your name: ") |
| 57 | + while not pass_weak: |
| 58 | + register_passwd = input("Input your password: ") |
| 59 | + if len(register_passwd) > 8: |
| 60 | + mycursor.execute(f"INSERT INTO system_data (name,password) VALUES ('{register_name}','{register_passwd}')") |
| 61 | + db.commit() |
| 62 | + pass_weak = True |
| 63 | + print("") |
| 64 | + print(f"Congratulations! Your name is {register_name} and your password is {register_passwd}!") |
| 65 | + else: |
| 66 | + print("Weak password, please type more than 8 characters!") |
| 67 | + elif user_input == str(3): |
| 68 | + |
| 69 | + mycursor.execute("SELECT * FROM system_data") |
| 70 | + credentials = [] |
| 71 | + for x in mycursor: |
| 72 | + credentials.append(x) |
| 73 | + while not login_status: |
| 74 | + login_name = input("Input your name: ") |
| 75 | + login_passwd = input("Input your password: ") |
| 76 | + if login_name == x[0]: |
| 77 | + if login_passwd == x[1]: |
| 78 | + print("") |
| 79 | + print("You successfully logged in, thank you really much for using our system :)") |
| 80 | + exit() |
| 81 | + else: |
| 82 | + print("Sorry, wrong password!") |
| 83 | + print(mycursor) |
| 84 | + else: |
| 85 | + print("Sorry, no such username!") |
0 commit comments