E-Banking System - Python & MySQL Code
This document contains the Python and SQL code for an E-Banking System. It demonstrates how to use
Python with MySQL to perform banking operations such as creating accounts, depositing, withdrawing,
transferring money, checking balances, and deleting accounts. The code is divided into two parts:
Python code and SQL code. The Python code communicates with MySQL to perform the required
operations. Below are the details and examples of using the banking system.
Python code for e banking
import mysql.connector
# Database connection setup
db = mysql.connector.connect(
host="localhost", # Your MySQL host
user="root", # Your MySQL username
password="mdali9835",# Your MySQL password
database="ebanking" # The database name
cursor = db.cursor()
# Function to create an account
def create_account(name, pin, initial_balance=0.0):
query = "INSERT INTO accounts (name, pin, balance) VALUES (%s, %s, %s)"
values = (name, pin, initial_balance)
cursor.execute(query, values)
db.commit()
print(f"Account created successfully! Account Number: {cursor.lastrowid}")
# Function to deposit money
def deposit(account_number, amount):
query = "UPDATE accounts SET balance = balance + %s WHERE account_number = %s"
cursor.execute(query, (amount, account_number))
db.commit()
print(f"Deposited {amount} successfully into account {account_number}.")
# Function to withdraw money
def withdraw(account_number, amount):
cursor.execute("SELECT balance FROM accounts WHERE account_number = %s", (account_number,))
balance = cursor.fetchone()
if balance and balance[0] >= amount:
query = "UPDATE accounts SET balance = balance - %s WHERE account_number = %s"
cursor.execute(query, (amount, account_number))
db.commit()
print(f"Withdrawn {amount} successfully from account {account_number}.")
else:
print("Insufficient balance!")
# Function to transfer money
def transfer(from_account, to_account, amount):
cursor.execute("SELECT balance FROM accounts WHERE account_number = %s", (from_account,))
balance = cursor.fetchone()
if balance and balance[0] >= amount:
try:
db.start_transaction()
cursor.execute("UPDATE accounts SET balance = balance - %s WHERE account_number = %s",
(amount, from_account))
cursor.execute("UPDATE accounts SET balance = balance + %s WHERE account_number = %s",
(amount, to_account))
db.commit()
print(f"Transferred {amount} from account {from_account} to account {to_account}.")
except:
db.rollback()
print("Transfer failed!")
else:
print("Insufficient balance!")
# Function to check balance
def check_balance(account_number):
cursor.execute("SELECT balance FROM accounts WHERE account_number = %s", (account_number,))
result = cursor.fetchone()
if result:
print(f"Account {account_number} balance: {result[0]}")
else:
print("Account not found!")
# Function to delete an account
def delete_account(account_number):
query = "DELETE FROM accounts WHERE account_number = %s"
cursor.execute(query, (account_number,))
db.commit()
print(f"Account {account_number} deleted successfully!")
# Main Program Execution
if __name__ == "__main__":
while True:
print("\n--- E-Banking System ---")
print("1. Create Account")
print("2. Deposit Money")
print("3. Withdraw Money")
print("4. Transfer Money")
print("5. Check Balance")
print("6. Delete Account")
print("7. Exit")
choice = input("Enter your choice: ")
if choice == '1':
name = input("Enter your name: ")
pin = int(input("Set a 4-digit PIN: "))
initial_balance = float(input("Enter initial balance (or leave as 0): "))
create_account(name, pin, initial_balance)
elif choice == '2':
account_number = int(input("Enter your account number: "))
amount = float(input("Enter the amount to deposit: "))
deposit(account_number, amount)
elif choice == '3':
account_number = int(input("Enter your account number: "))
amount = float(input("Enter the amount to withdraw: "))
withdraw(account_number, amount)
elif choice == '4':
from_account = int(input("Enter your account number: "))
to_account = int(input("Enter the recipient's account number: "))
amount = float(input("Enter the amount to transfer: "))
transfer(from_account, to_account, amount)
elif choice == '5':
account_number = int(input("Enter your account number: "))
check_balance(account_number)
elif choice == '6':
account_number = int(input("Enter the account number to delete: "))
delete_account(account_number)
elif choice == '7':
print("Exiting E-Banking System. Goodbye!")
break
else:
print("Invalid choice! Please try again.")
# Close the connection when the program ends
cursor.close()
db.close()
SAMPLE CODE
CREATING ACCOUNT
Checking Balance