Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from myconnection import connect_to_mysql
- from mysql.connector import Error
- config = {
- "host": "127.0.0.1",
- "user": "root",
- "password": "",
- "database": "bankdb",
- }
- def execute_query(connection, query, values=None):
- try:
- with connection.cursor() as cursor:
- cursor.execute(query, values)
- connection.commit()
- print("Query executed successfully")
- except Error as e:
- print(f"Error: {e}")
- def fetch_query(connection, query):
- try:
- with connection.cursor() as cursor:
- cursor.execute(query)
- return cursor.fetchall()
- except Error as e:
- print(f"Error: {e}")
- def insert_account(connection, account_no, password, name, amount, phone):
- query = "INSERT INTO accounts(account_no, password, name, amount, phone) VALUES (%s, %s, %s, %s, %s)"
- values = (account_no, password, name, amount, phone)
- execute_query(connection, query, values)
- def signup(connection):
- account_no = int(input('Enter account no: '))
- password = input('Enter Password: ')
- confirm_password = input('Enter Confirm Password: ')
- if len(password) < 8 or password != confirm_password:
- print('Password must be at least 8 characters and match the confirmation.')
- return
- name = input('Enter your name: ')
- amount = int(input('Enter Initial Deposit amount: '))
- phone = input('Enter your phone no: ')
- insert_account(connection, account_no, password, name, amount, phone)
- def deposit_money(connection, account_no, deposit_amount):
- query = 'UPDATE accounts SET amount = amount + %s WHERE account_no = %s'
- execute_query(connection, query, (deposit_amount, account_no))
- def withdraw_money(connection, account_no, withdraw_amount):
- query = 'UPDATE accounts SET amount = amount - %s WHERE account_no = %s AND amount >= %s'
- values = (withdraw_amount, account_no, withdraw_amount)
- execute_query(connection, query, values)
- def show_user_accounts(connection, account_no):
- query = 'SELECT * FROM accounts WHERE account_no = %s'
- data = fetch_query(connection, query, (account_no,))
- for row in data:
- print(row)
- def signin(connection):
- account_no = int(input('Enter account no: '))
- password = input('Enter Password: ')
- query = 'SELECT * FROM accounts WHERE account_no = %s AND password = %s'
- data = fetch_query(connection, query, (account_no, password))
- if data:
- print('Successfully Signin!')
- return True, account_no
- else:
- print('Wrong username or password.')
- return False, None
- def main():
- connection = connect_to_mysql(config, attempts=3)
- if not connection or not connection.is_connected():
- print('Connection error!, Bye')
- return
- while True:
- sign_in_status, account_no = signin(connection)
- if sign_in_status:
- break
- ans = input('Try again? (y/n): ')
- if ans.lower() != 'y':
- print('Bye!')
- return
- while True:
- print('0-Exit')
- print('1-Create Account')
- print('2-Deposit Money')
- print('3-Withdraw Money')
- print('4-Show Amount')
- ans = input('Enter your choice(0,1,2,3,4): ')
- if ans == '0':
- print('Bye')
- connection.close()
- return
- elif ans == '1':
- print('Welcome:')
- signup(connection)
- elif ans == '2':
- deposit_amount = int(input('Enter amount to deposit: '))
- deposit_money(connection, account_no, deposit_amount)
- elif ans == '3':
- withdraw_amount = int(input('Enter amount to withdraw: '))
- withdraw_money(connection, account_no, withdraw_amount)
- elif ans == '4':
- show_user_accounts(connection, account_no)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement