这是一个使用python连接mysql的例子

涉及到类的使用

import pymysql
import function as f
def mysql():
db=pymysql.connect(host='localhost',user='root',password='1234',charset='utf8')
cursor=db.cursor()
cursor.execute('create database bank;')
cursor.execute('use bank')
sql='''
create table account(account_id varchar(50),
account_passwd char(6),
money decimal(10,2)
)default charset=utf8;
'''
cursor.execute(sql)
sql_insert='''
insert into account values('supermao','200112',10000.00),
('other','123',100.00);
'''
cursor.execute(sql_insert)
db.commit() def start():
while True:
num=input('欢迎来到银行管理系统 1.登录 2.注册 q.退出:')
if num == '2':
account = f.Account(None,None)
account.touch_character()
print('注册成功')
elif num == 'q':
print('已退出')
break
elif num == '1':
name=input('请输入用户名:')
passwd=input('请输入密码:')
account = f.Account(name, passwd)
account.check_id()
if account.choice == '5':
continue
while True:
account.main()
if account.choice == '4':
break
else:
print('请重新输入')
continue #mysql()
start()

function.py

import pymysql
db=None
class Account:
def __init__(self,user,passwd):
self.user=user
self.passwd=passwd
self.money_sql="select money from account where account_id='%s' and account_passwd='%s'"\
%(self.user,self.passwd) def main(self):
global db
db=pymysql.connect(host='localhost', user='root',
password='1234', charset='utf8',database='bank')
self.cursor = db.cursor()
print('1.取钱, 2.查询, 3.存钱, 4.退出')
choice=input('请按序号进行选择:')
if choice == '1':
self.withdraw()
self.choice = choice
if choice == '2':
self.select()
self.choice = choice
if choice == '3':
self.save_money()
self.choice = choice
if choice == '4':
self.exit()
self.choice = choice
def withdraw(self):
self.cursor.execute(self.money_sql)
money=self.cursor.fetchall()
fetch=input('取多少钱?')
self.fin=int(money[0][0])-int(fetch)
if self.fin < 0:
print('余额不足')
else:
update_sql = 'UPDATE account set money=%d where account_id="%s" ' \
'and account_passwd="%s"' % (self.fin, self.user, self.passwd)
self.cursor.execute(update_sql)
db.commit() def select(self):
self.cursor.execute(self.money_sql)
money=self.cursor.fetchall()
print('当前余额%s'%(money[0][0])) def save_money(self):
self.cursor.execute(self.money_sql)
money=self.cursor.fetchall()
save=input('存多少钱')
total=int(money[0][0])+int(save)
update_sql2 = "UPDATE account set money=%d where account_id='%s' ' \
'and account_passwd='%s'" % (total, self.user, self.passwd)
self.cursor.execute(update_sql2)
db.commit()
def touch_character(self):
db = pymysql.connect(host='localhost', user='root',
password='1234', charset='utf8', database='bank')
cursor=db.cursor()
user=input('创建用户')
passwd=input('输入用户密码')
sql='''insert into account values('%s','%s',0);'''%(user,passwd)
cursor.execute(sql)
db.commit() def check_id(self):
db=pymysql.connect(host='localhost', user='root',
password='1234', charset='utf8',database='bank')
self.cursor=db.cursor()
select = '''select * from account where account_id='%s'
and account_passwd='%s';''' % (self.user, self.passwd)
self.cursor.execute('use bank')
self.cursor.execute(select)
if self.cursor.fetchall():
print(' 登录成功')
print('你好%s'%(self.user))
self.choice = '1'
else:
print(' 登录失败')
self.choice = '5' def exit(self):
self.cursor.close()
db.close()

python 银行管理系统的更多相关文章

  1. 银行管理系统[C++]

    //项目:银行管理系统 //系统实现的主要有管理,取款机管理,用户查询等功能: //*管理模块:存款.取款.开户.销户.修改信息.办卡.挂失卡; //*用户查询模块; //*取款机信息管理模块:管理员 ...

  2. c++ 银行管理系统及报告

    1.题目描写叙述: 本代码为银行管理系统,总体分为管理员模式和普通用户模式: (1)在管理员模式中能完毕 ①用户信息录入 ②改动管理员password ③改动指定账户信息 ④信息管理业务 (2)在普通 ...

  3. python连接mysql数据库实例demo(银行管理系统数据库版)

    主函数: import adminView import os import pickle from bankFunction import BankFunction import time def ...

  4. python用户管理系统

    学Python这么久了,第一次写一个这么多的代码(我承认只有300多行,重复的代码挺多的,我承认我确实垃圾),但是也挺不容易的 自定义函数+装饰器,每一个模块写的一个函数 很多地方能用装饰器(逻辑跟不 ...

  5. python名片管理系统V2

    主程序: #! /usr/bin env python3 # -*- coding: utf-8 -*- # 项目三: # 1.要求:编写一个名片管理系统,功能如下: # 用户输入相对应的指令,实现对 ...

  6. python名片管理系统

    1.代码: (1)主程序 #!/usr/bin/env python # -*- coding: UTF-8 -*- import cards_tools # 无限循环,由用户主动决定什么时候退出循环 ...

  7. python学生管理系统

    import osimport re #获取本机用户名,构建student.txt文件名创建在左面import getpassusername=getpass.getuser()print(" ...

  8. python员工管理系统(基础版)

    各位小伙伴们大家好,最近自学python已经差不多把基础学完了,现在运用这些基础知识写了一个简单版本的员工管理系统 因为在大学里我是学过java的,所以,当有了一定的基础和熏陶外,真心觉得python ...

  9. Python图书管理系统

    图书管理系统 功能简介 添加图书时,图书ID不能重复,图书名可重复 删除,查询,修改功能,输入图书名之后提供所有的同名的图书,用户可以按照图书序号对具体的一本书进行操作 显示书籍,分行显示,每行一本书 ...

随机推荐

  1. Redis 很屌,不懂使用规范就糟蹋了

    这可能是最中肯的 Redis 使用规范了 码哥,昨天我被公司 Leader 批评了. 我在单身红娘婚恋类型互联网公司工作,在双十一推出下单就送女朋友的活动. 谁曾想,凌晨 12 点之后,用户量暴增,出 ...

  2. Codeforces 1304F1/F2 Animal Observation(单调队列优化 dp)

    easy 题目链接 & hard 题目链接 给出一张 \(n \times m\) 的矩阵,每个格子上面有一个数,你要在每行选出一个点 \((i,t)\),并覆盖左上角为 \((i,t)\), ...

  3. 【2020五校联考NOIP #8】自闭

    题目传送门 题意: 有一个 \(n \times m\) 的矩阵,里面已经填好了 \(k\) 个非负整数. 问是否能在其它 \(n \times m-k\) 个格子里各填上一个非负整数,使得得到的矩阵 ...

  4. 洛谷 P5643 - [PKUWC2018]随机游走(Min-Max 容斥+FWT+树上高斯消元,hot tea)

    题面传送门 一道挺综合的 hot tea,放到 PKUWC 的 D2T2 还挺喜闻乐见的( 首先我们考虑怎样对一个固定的集合 \(S\) 计算答案,注意到我们要求的是一个形如 \(E(\max(S)) ...

  5. azkaban执行任务长时间无法结束

    问题显示: 由于一次执行较多的任务,导致azkaban的web程序崩溃,此时,关闭azkaban服务,重新启动azkaban 但是由于azkaban的exec程序无法关闭,这里采用kill的方式关掉e ...

  6. halt

    halt命令用来关闭正在运行的Linux操作系统.halt命令会先检测系统的runlevel,若runlevel为0或6,则关闭系统,否则即调用shutdown来关闭系统. 语法 halt(选项) 选 ...

  7. 学习 DDD - 通用语言的模式

    大家好,我是霸戈,这周学习了一些关于领域驱动设计的知识 ,对比较深刻的地方做了不少笔记,分享给大家. 在日常需求讨论的时候,经常会碰到一个需求会议开了一个多小时还没有达成共识.作为业务方(领域专家)明 ...

  8. euerka总结

    一.euerka的基本知识 1. 服务治理 Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务治理 在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系 ...

  9. HDFS05 NameNode和SecondaryNameNode

    NameNode和SecondaryNameNode(了解) 目录 NameNode和SecondaryNameNode(了解) NN 和 2NN 工作机制 NameNode工作机制 Secondar ...

  10. A Child's History of England.31

    The English in general were on King Henry's side, though many of the Normans were on Robert's. But t ...