Advertisement
DrAungWinHtut

mybank.py

Jun 4th, 2024
471
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.60 KB | None | 0 0
  1. # error code 1 - connection error
  2. # error code 2 - user error, username or password
  3.  
  4.  
  5. from myconnection import connect_to_mysql #pip install mysql-connector-python, myconnection.py must exists
  6. from icecream import ic #pip install icecream
  7. from mysql.connector import Error
  8.  
  9. config = {
  10.     "host": "127.0.0.1",
  11.     "user": "root",
  12.     "password": "",
  13.     "database": "bankdb",
  14. }
  15.  
  16. user_signIn = False
  17. admin_signIn = False
  18. acc = 0
  19.  
  20. # Function to execute a query
  21. def execute_query(connection, query, values=None):
  22.     cursor = connection.cursor()
  23.     try:
  24.         if values:
  25.             cursor.execute(query, values)
  26.         else:
  27.             cursor.execute(query)
  28.         connection.commit()
  29.         print("Query executed successfully")
  30.     except Error as e:
  31.         print(f"The error '{e}' occurred")
  32.  
  33. # Function to fetch results from a select query
  34. def fetch_query(connection, query):
  35.     cursor = connection.cursor()
  36.     result = None
  37.     try:
  38.         cursor.execute(query)
  39.         result = cursor.fetchall()
  40.         return result
  41.     except Error as e:
  42.         print(f"The error '{e}' occurred")
  43.  
  44.  
  45. # INSERT
  46. def insertData(connection,accountno,password,name,amount,phone):
  47.     q = f"INSERT INTO accounts(account_no,password,name,amount,phone) values ({accountno},'{password}','{name}',{amount},'{phone}')"
  48.     # string must be quoted (အပြင်မှာ '' ဆို အတွင်းမှာ "")
  49.     execute_query(connection,q)
  50.  
  51. # Signup
  52. def signup(connection):
  53.     accountno = input('Enter account no: ')
  54.     accountno = int(accountno)
  55.  
  56.     while True:
  57.         password = input('Enter Password: ')
  58.         confirm_password = input('Enter Confirm Password: ')
  59.         if len(password) < 8:
  60.             print('Passowrd must at least 8 digits')
  61.         elif password != confirm_password:
  62.             print('Passwords did not much, Please try again!')
  63.         else:
  64.             break
  65.  
  66.     name = input('Enter your name: ')
  67.     amount = input('Enter Initial Deposit amount: ')
  68.     amount = int(amount)
  69.     phone = input('Enter your phone no: ')
  70.     insertData(connection,accountno,password,name,amount,phone)
  71.  
  72. # DELETE
  73. def deleteData(connection,accountno):
  74.     q = f"DELETE from accounts where accountno = {accountno}"
  75.     execute_query(connection,q)
  76.  
  77. # UPDATE
  78. def updateAmount(connection,accountno,amount):
  79.     q = f"UPDATE accounts SET amount = {amount} where accountno = {accountno}"
  80.     execute_query(connection,q)
  81.  
  82. def depositMoney(connection,account_no,depositAmount):
  83.     q = f'SELECT amount from accounts where account_no = {account_no}'
  84.     data = fetch_query(connection,q)
  85.     amount = data[0][0]
  86.     amount = amount + depositAmount
  87.     updateAmount(connection,account_no,amount)
  88.     print(f'Now your amount is updated: {amount}')
  89.  
  90.  
  91.  
  92. # readAllAccounts
  93. def showAllAccounts(connection):
  94.     q = 'SELECT * from accounts'
  95.     execute_query(connection,q)
  96.  
  97. def checkAccount(connection,account_no,password):
  98.     q = f'SELECT * from accounts where account_no = {account_no} and password = "{password}"'
  99.     data = fetch_query(connection,q)
  100.     #ic(data)
  101.     if data != []:
  102.         checkAdmin(connection,account_no)          
  103.         return True    
  104.          
  105.     else:        
  106.         return False
  107.    
  108. def checkAdmin(connection,account_no):
  109.     global admin_signIn
  110.     global user_signIn
  111.     q = f'SELECT id from accounts where account_no = {account_no}'
  112.     data = fetch_query(connection,q)
  113.     id = data[0][0]
  114.     ic(id)
  115.     q = f'SELECT * from admin where accounts_id = {id}'
  116.     data = fetch_query(connection,q)
  117.     ic(data)
  118.     if data != []:  
  119.         admin_signIn = True
  120.         user_signIn = False
  121.         print('Admin Login')            
  122.          
  123.     else:
  124.         print('User Login')
  125.         admin_signIn = False
  126.         user_signIn = True
  127.    
  128.  
  129. # showThatAcc
  130. def showUserAccounts(connection,accountno):
  131.     q = f'SELECT * from accounts where accountno = {accountno}'
  132.     data = fetch_query(connection,q)
  133.     for d in data:
  134.         print(d)
  135.  
  136.  
  137. def signIn(connection):
  138.     global acc
  139.     acc = input('Enter account no: ')
  140.     acc = int(acc)
  141.     password = input('Enter Password: ')
  142.     status = checkAccount(connection,acc,password)
  143.     if status:
  144.         print('Successfully Signin!')
  145.         return True
  146.     else:
  147.         print('Wrong username or password.')
  148.         return False
  149.  
  150. #program start here
  151. cnx = connect_to_mysql(config, attempts=3)
  152.  
  153. if not cnx or not cnx.is_connected():
  154.     print('Connection error!, Bye')
  155.     exit(1)
  156.  
  157. # Database connection OK...
  158. while True:
  159.     signIn_status = signIn(cnx)
  160.     if signIn_status:
  161.         break
  162.     ans = input('Try again? (y/n): ')
  163.     if ans.lower() != 'y':
  164.         print('Bye!')
  165.         exit(0)
  166.  
  167. #Menu
  168. while True:
  169.     print('0-Exit')
  170.     print('1-Create Account') #must member of Admin to create account
  171.     print('2-deposit Money')
  172.     print('3-Withdraw Money')
  173.     print('4-Show Amount')
  174.     print('5-Show All Users (for admin only)') #must member of Admin to create account
  175.     print('6-Delete Account') #must member of Admin to create account
  176.  
  177.     ans = input('Enter your choice(0,1,2,3,4,5,6): ')
  178.     ans = int(ans)
  179.     if ans == 0:
  180.         print('Bye')
  181.         cnx.close()
  182.         exit(0)
  183.     elif ans == 1:
  184.         if admin_signIn:
  185.             print('Welcome Admin:')
  186.             signup(cnx)
  187.         else:
  188.             print('You dont have priveleges to create an account!')
  189.     elif ans == 2:        
  190.         depositAmount = input('Pls enter amount to deposit: ')
  191.         depositAmount = int(depositAmount)
  192.         depositMoney(cnx,acc,depositAmount)
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
Advertisement
Comments
  • UNBZN
    170 days
    # text 0.81 KB | 0 0
    1. You can add in your code a hash protection for password and a protection for SQL injection :
    2.  
    3.  
    4. Protection SQL :
    5.  
    6. # INSERT avec protection contre les injections SQL
    7. def insertData(connection, accountno, password, name, amount, phone):
    8. q = "INSERT INTO accounts(account_no, password, name, amount, phone) VALUES (%s, %s, %s, %s, %s)"
    9. values = (accountno, password, name, amount, phone)
    10. execute_query(connection, q, values)
    11.  
    12. Hash protection for password :
    13.  
    14. import bcrypt
    15.  
    16. # Hachage du mot de passe
    17. def hash_password(password):
    18. salt = bcrypt.gensalt()
    19. return bcrypt.hashpw(password.encode('utf-8'), salt)
    20.  
    21. # Dans la fonction signup, remplacez la ligne d'insertion par :
    22. hashed_password = hash_password(password)
    23. insertData(connection, accountno, hashed_password, name, amount, phone)
    24.  
Add Comment
Please, Sign In to add comment
Advertisement