Advertisement
DrAungWinHtut

mysqlbankplain.py

Jun 11th, 2024
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.82 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 deleteAccount(connection,accountno):
  74.     q = f"DELETE from accounts where account_no = {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. def withdrawMoney(connection,account_no,withdrawAmount):
  91.     q = f'SELECT amount from accounts where account_no = {account_no}'
  92.     data = fetch_query(connection,q)
  93.     amount = data[0][0]
  94.     if withdrawAmount > amount:
  95.         print('Insufficient amount!')
  96.         return(3)
  97.     amount = amount - depositAmount
  98.     updateAmount(connection,account_no,amount)
  99.     print(f'Now your amount is updated: {amount}')
  100.  
  101.  
  102. # readAllAccounts
  103. def showAllAccounts(connection):
  104.     q = 'SELECT * from accounts'
  105.     data = fetch_query(connection,q)
  106.     print('\n\nUser Accounts')
  107.     print('-------------')
  108.     for d in data:
  109.         print(f'id = {d[0]}, account_no = {d[1]}, name = {d[2]}, amount = {d[3]}, phone = {d[4]}')
  110.  
  111. def checkAccount(connection,account_no,password):
  112.     q = f'SELECT * from accounts where account_no = {account_no} and password = "{password}"'
  113.     data = fetch_query(connection,q)
  114.     #ic(data)
  115.     if data != []:
  116.         checkAdmin(connection,account_no)          
  117.         return True    
  118.          
  119.     else:        
  120.         return False
  121.    
  122. def checkAdmin(connection,account_no):
  123.     global admin_signIn
  124.     global user_signIn
  125.     q = f'SELECT id from accounts where account_no = {account_no}'
  126.     data = fetch_query(connection,q)
  127.     id = data[0][0]
  128.     #ic(id)
  129.     q = f'SELECT * from admin where accounts_id = {id}'
  130.     data = fetch_query(connection,q)
  131.     #ic(data)
  132.     if data != []:  
  133.         admin_signIn = True
  134.         user_signIn = False
  135.         print('Admin Login')            
  136.          
  137.     else:
  138.         print('User Login')
  139.         admin_signIn = False
  140.         user_signIn = True
  141.    
  142.  
  143. # showThatAcc
  144. def showUserAccounts(connection,accountno):
  145.     q = f'SELECT * from accounts where accountno = {accountno}'
  146.     data = fetch_query(connection,q)
  147.     for d in data:
  148.         print(d)
  149.  
  150.  
  151. def signIn(connection):
  152.     global acc
  153.     acc = input('Enter account no: ')
  154.     acc = int(acc)
  155.     password = input('Enter Password: ')
  156.     status = checkAccount(connection,acc,password)
  157.     if status:
  158.         print('Successfully Signin!')
  159.         return True
  160.     else:
  161.         print('Wrong username or password.')
  162.         return False
  163.  
  164. #program start here
  165. cnx = connect_to_mysql(config, attempts=3)
  166.  
  167. if not cnx or not cnx.is_connected():
  168.     print('Connection error!, Bye')
  169.     exit(1)
  170.  
  171. # Database connection OK...
  172. while True:
  173.     signIn_status = signIn(cnx)
  174.     if signIn_status:
  175.         break
  176.     ans = input('Try again? (y/n): ')
  177.     if ans.lower() != 'y':
  178.         print('Bye!')
  179.         exit(0)
  180.  
  181. #Menu
  182. while True:
  183.     print('0-Exit')
  184.     print('1-Create Account') #must member of Admin to create account
  185.     print('2-deposit Money')
  186.     print('3-Withdraw Money')
  187.     print('4-Show Amount')
  188.    
  189.     if admin_signIn:
  190.         print('5-Show All Users (for admin only)') #must member of Admin to create account
  191.         print('6-Delete Account (for admin only)') #must member of Admin to create account
  192.         ans = input('Enter your choice(0,1,2,3,4,5,6): ')
  193.     else:
  194.         ans = input('Enter your choice(0,1,2,3,4): ')
  195.  
  196.     ans = int(ans)
  197.     if ans == 0:
  198.         print('Bye')
  199.         cnx.close()
  200.         exit(0)
  201.     elif ans == 1:
  202.         if admin_signIn:
  203.             print('Welcome Admin:')
  204.             signup(cnx)
  205.         else:
  206.             print('You dont have priveleges to create an account!')
  207.     elif ans == 2:        
  208.         depositAmount = input('Pls enter amount to deposit: ')
  209.         depositAmount = int(depositAmount)
  210.         depositMoney(cnx,acc,depositAmount)
  211.     elif ans == 3:        
  212.         wdAmount = input('Pls enter amount to withdraw: ')
  213.         wdAmount = int(wdAmount)
  214.         withdrawMoney(cnx,acc,wdAmount)
  215.     elif ans == 4:    
  216.         showUserAccounts(cnx,acc)
  217.     elif ans == 5:    
  218.         if not admin_signIn:
  219.             print('You are not Admin!, Sorry!')
  220.         showAllAccounts(cnx)
  221.     elif ans == 6:    
  222.         acc2Del = input('Enter account no. to delete: ')
  223.         acc2Del = int(acc2Del)
  224.         deleteAccount(cnx,acc2Del)
  225.    
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement