简易银行系统
简介
本博文将通过简易的Python与数据库间联动,实现简易的银行系统。该系统主要功能有用户注册,用户登录,余额查询,存取款以及转账。
代码实现:
1、数据库的建立
-
进入MySQL环境
mysql -h MySQL服务器地址 -u 用户名 -p -
显示所有数据库
show databases;
3.创建数据库
create database 数据库名称 [default character set ‘utf8’];
或者
create database 数据库名称 [charset='utf8‘];
这里的数据库我定义为mydb. -
进入某个数据库
use mydb(数据库名称);
4.创建表create table 表名称(
字段名1 数据类型 约束[primary key],
字段名2 数据类型 约束…,
字段名3 数据类型 约束…
);
create table account(
name varchar(20) not null,
password int(20) not null,
money float not null,
);
4.向表中插入记录
语法一(指定字段名插入记录):
insert into 表名称(字段A,字段B…)values(值A,值B…);
语法二:(不指定字段插入记录,插入的值对应于表 中的每个字段)
insert into 表名称 values(值1,值2,…);
语法三:(插入多条记录)
insert into 表名称(字段A,字段B…)values(值A1,值B1…),(值A2,值B2…)…;
完成初始数据录入。
5. 显示所有表
show tables;
2、Python方面实现功能
在这import time
import pymysql
import numpy as np
import subprocess
if __name__ == '__main__':
connect = pymysql.connect(
host="localhost",
port=3306,
user="root",
password="123456",
database="mydb",
charset="utf8"
)
cursor = connect.cursor()
sql = "select * from account"
cursor.execute(sql)
items = cursor.fetchall()
print("********XX银行系统***********")
for i in range(len(items)):
item = items[i][0]
print("用户姓名", item)
person=items[:][0]
while True:
choice1 = input("1 注册 2登录 3 退出")
if choice1 == "1":
enroll_name=input("输入注册人姓名")
enroll_pasword=input("输入注册密码")
sql1="insert into account values('{}',{},'{}')".format(enroll_name,0,enroll_pasword)
cursor.execute(sql1)
connect.commit()
time.sleep(1)
print("注册成功!")
time.sleep(30)
subprocess.call("cls", shell=True)
elif choice1 == "2":
enter_person = input("操作人:")
enter_password = input("输入密码:")
sql2="select * from account where name='{}' and password='{}'".format(enter_person,enter_password)
cursor.execute(sql2)
ce = cursor.fetchone()
connect.commit()
if ce is not None:
choice2 = input("1 存钱 2 取钱 3 转账 4查询余额 5 退出")
if choice2 == "1":
print("请输入存入的钱数:")
money = input()
sql3 = "select money from account where name='{}' ".format(enter_person)
cursor.execute(sql3)
ce1 = cursor.fetchone()
print("当前余额(存钱之前):", float(*ce1))
print("现在余额(存钱之后):", float(*ce1) + float(money))
sql4 = "update account set money={} where name='{}'".format(
float(*ce1) + float(money), enter_person)
cursor.execute(sql4)
connect.commit()
time.sleep(30)
subprocess.call("cls", shell=True)
elif choice2 == "2":
print("请输入取出的钱数:")
money = input()
sql5 = "select money from account where name='{}' ".format(enter_person)
cursor.execute(sql5)
ce2 = cursor.fetchone()
print("当前余额(取钱之前):", float(*ce2))
print("现在余额(取钱之后):", float(*ce2) - float(money))
sql6 = "update account set money={} where name='{}'".format(
float(*ce2) - float(money), enter_person)
cursor.execute(sql6)
connect.commit()
time.sleep(30)
subprocess.call("cls", shell=True)
elif choice2 == "3":
get_person = input("选择收账人")
sql7 = "select money from account where name='{}' ".format(get_person)
cursor.execute(sql7)
ce3 = cursor.fetchone()
if ce3 is not None:
print("输入转账金额")
money = input()
sql8 = "select money from account where name='{}' ".format(enter_person)
cursor.execute(sql8)
ce4 = cursor.fetchone()
print("当前余额(转账之前):", float(*ce4))
print("现在余额(转账之后):", float(*ce4) - float(money))
sql9 = "update account set money={} where name='{}'".format(
float(*ce4) - float(money), enter_person)
sql10 = "update account set money={} where name='{}'".format(
float(*ce3) + float(money), get_person)
cursor.execute(sql9)
cursor.execute(sql10)
connect.commit()
time.sleep(30)
subprocess.call("cls", shell=True)
else:
print("查无此人")
time.sleep(10)
subprocess.call("cls", shell=True)
elif choice2 == "4":
sql11 = "select money from account where name='{}' ".format(enter_person)
cursor.execute(sql11)
ce5 = cursor.fetchone()
connect.commit()
print("余额为", float(*ce5))
time.sleep(30)
subprocess.call("cls",shell=True)
elif choice2 == "5":
break
else:
print("无该用户或密码错误")
elif choice1 == "3":
cursor.close()
connect.close()
break
else:
print("无效操作请重试")里插入代码片
3、结果展示