Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # error code 1 - connection error
- # error code 2 - user error, username or password
- from myconnection import connect_to_mysql #pip install mysql-connector-python, myconnection.py must exists
- from icecream import ic #pip install icecream
- from mysql.connector import Error
- config = {
- "host": "127.0.0.1",
- "user": "root",
- "password": "",
- "database": "bankdb",
- }
- user_signIn = False
- admin_signIn = False
- acc = 0
- # Function to execute a query
- def execute_query(connection, query, values=None):
- cursor = connection.cursor()
- try:
- if values:
- cursor.execute(query, values)
- else:
- cursor.execute(query)
- connection.commit()
- print("Query executed successfully")
- except Error as e:
- print(f"The error '{e}' occurred")
- # Function to fetch results from a select query
- def fetch_query(connection, query):
- cursor = connection.cursor()
- result = None
- try:
- cursor.execute(query)
- result = cursor.fetchall()
- return result
- except Error as e:
- print(f"The error '{e}' occurred")
- # INSERT
- def insertData(connection,accountno,password,name,amount,phone):
- q = f"INSERT INTO accounts(account_no,password,name,amount,phone) values ({accountno},'{password}','{name}',{amount},'{phone}')"
- # string must be quoted (အပြင်မှာ '' ဆို အတွင်းမှာ "")
- execute_query(connection,q)
- # Signup
- def signup(connection):
- accountno = input('Enter account no: ')
- accountno = int(accountno)
- while True:
- password = input('Enter Password: ')
- confirm_password = input('Enter Confirm Password: ')
- if len(password) < 8:
- print('Passowrd must at least 8 digits')
- elif password != confirm_password:
- print('Passwords did not much, Please try again!')
- else:
- break
- name = input('Enter your name: ')
- amount = input('Enter Initial Deposit amount: ')
- amount = int(amount)
- phone = input('Enter your phone no: ')
- insertData(connection,accountno,password,name,amount,phone)
- # DELETE
- def deleteData(connection,accountno):
- q = f"DELETE from accounts where accountno = {accountno}"
- execute_query(connection,q)
- # UPDATE
- def updateAmount(connection,accountno,amount):
- q = f"UPDATE accounts SET amount = {amount} where accountno = {accountno}"
- execute_query(connection,q)
- def depositMoney(connection,account_no,depositAmount):
- q = f'SELECT amount from accounts where account_no = {account_no}'
- data = fetch_query(connection,q)
- amount = data[0][0]
- amount = amount + depositAmount
- updateAmount(connection,account_no,amount)
- print(f'Now your amount is updated: {amount}')
- # readAllAccounts
- def showAllAccounts(connection):
- q = 'SELECT * from accounts'
- execute_query(connection,q)
- def checkAccount(connection,account_no,password):
- q = f'SELECT * from accounts where account_no = {account_no} and password = "{password}"'
- data = fetch_query(connection,q)
- #ic(data)
- if data != []:
- checkAdmin(connection,account_no)
- return True
- else:
- return False
- def checkAdmin(connection,account_no):
- global admin_signIn
- global user_signIn
- q = f'SELECT id from accounts where account_no = {account_no}'
- data = fetch_query(connection,q)
- id = data[0][0]
- ic(id)
- q = f'SELECT * from admin where accounts_id = {id}'
- data = fetch_query(connection,q)
- ic(data)
- if data != []:
- admin_signIn = True
- user_signIn = False
- print('Admin Login')
- else:
- print('User Login')
- admin_signIn = False
- user_signIn = True
- # showThatAcc
- def showUserAccounts(connection,accountno):
- q = f'SELECT * from accounts where accountno = {accountno}'
- data = fetch_query(connection,q)
- for d in data:
- print(d)
- def signIn(connection):
- global acc
- acc = input('Enter account no: ')
- acc = int(acc)
- password = input('Enter Password: ')
- status = checkAccount(connection,acc,password)
- if status:
- print('Successfully Signin!')
- return True
- else:
- print('Wrong username or password.')
- return False
- #program start here
- cnx = connect_to_mysql(config, attempts=3)
- if not cnx or not cnx.is_connected():
- print('Connection error!, Bye')
- exit(1)
- # Database connection OK...
- while True:
- signIn_status = signIn(cnx)
- if signIn_status:
- break
- ans = input('Try again? (y/n): ')
- if ans.lower() != 'y':
- print('Bye!')
- exit(0)
- #Menu
- while True:
- print('0-Exit')
- print('1-Create Account') #must member of Admin to create account
- print('2-deposit Money')
- print('3-Withdraw Money')
- print('4-Show Amount')
- print('5-Show All Users (for admin only)') #must member of Admin to create account
- print('6-Delete Account') #must member of Admin to create account
- ans = input('Enter your choice(0,1,2,3,4,5,6): ')
- ans = int(ans)
- if ans == 0:
- print('Bye')
- cnx.close()
- exit(0)
- elif ans == 1:
- if admin_signIn:
- print('Welcome Admin:')
- signup(cnx)
- else:
- print('You dont have priveleges to create an account!')
- elif ans == 2:
- depositAmount = input('Pls enter amount to deposit: ')
- depositAmount = int(depositAmount)
- depositMoney(cnx,acc,depositAmount)
Advertisement
Comments
-
- You can add in your code a hash protection for password and a protection for SQL injection :
- Protection SQL :
- # INSERT avec protection contre les injections SQL
- def insertData(connection, accountno, password, name, amount, phone):
- q = "INSERT INTO accounts(account_no, password, name, amount, phone) VALUES (%s, %s, %s, %s, %s)"
- values = (accountno, password, name, amount, phone)
- execute_query(connection, q, values)
- Hash protection for password :
- import bcrypt
- # Hachage du mot de passe
- def hash_password(password):
- salt = bcrypt.gensalt()
- return bcrypt.hashpw(password.encode('utf-8'), salt)
- # Dans la fonction signup, remplacez la ligne d'insertion par :
- hashed_password = hash_password(password)
- insertData(connection, accountno, hashed_password, name, amount, phone)
Add Comment
Please, Sign In to add comment
Advertisement