这是一个使用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. 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明

    目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...

  2. File与IO基础

    IO流的作用:持久化到磁盘 File类的使用 File类基本概念 文件和文件夹都是用File类来表示. File类是内存层面的对象,内存中创建出来的File对象不一定有一个真实存在的文件或文件夹,但是 ...

  3. 洛谷 P5332 - [JSOI2019]精准预测(2-SAT+bitset+分块处理)

    洛谷题面传送门 七月份(7.31)做的题了,题解到现在才补,不愧是 tzc 首先不难发现题目中涉及的变量都是布尔型变量,因此可以考虑 2-SAT,具体来说,我们将每个人在每个时刻的可能的状态表示出来. ...

  4. Tarjan 的一些板子

    圆方树(会在两圆点间建方点): void tarjan(int u) { low[u] = dfn[u] = ++dfc, stk[++top] = u, num++; for (int v : G[ ...

  5. PDO的好处

    产生原因 普通的SQL执行语句,由于研发人员对前端请求参数过滤不严谨,导致SQL被注入,从而影响数据库,带来风险 使用PDO后形成的语句 SELECT * FROM test WHERE id in ...

  6. shell命令行——快捷键

    生活在 Bash shell 中,熟记以下快捷键,将极大的提高你的命令行操作效率. 编辑命令 Ctrl + a :移到命令行首 Ctrl + e :移到命令行尾 Ctrl + f :按字符前移(右向) ...

  7. Python——MacBook Pro中安装pip

    1.系统已有python2和python3,如何检查MacBook Pro系统是否安装的有pip? 看到terminal的提示没有,有提示pip的,下面的提示,说明pip安装了. 要查看pip3是否安 ...

  8. Spark(七)【RDD的持久化Cache和CheckPoint】

    RDD的持久化 1. RDD Cache缓存 ​ RDD通过Cache或者Persist方法将前面的计算结果缓存,默认情况下会把数据以缓存在JVM的堆内存中.但是并不是这两个方法被调用时立即缓存,而是 ...

  9. Oracle中的job(定时任务)

    oracle中的job(定时任务)由dbms_job包下的函数实现.关于job的理论知识可参考https://blog.csdn.net/apextrace/article/details/77675 ...

  10. SpringMVC详细实例

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...