这是一个使用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. Codeforces 1464F - My Beautiful Madness(树的直径)

    Codeforces 题面传送门 & 洛谷题面传送门 树上数据结构大杂烩(?) 首先考虑什么样的点能够在所有路径的 \(d\) 邻居的交集内.显然如果一个点在一条路径的 \(d\) 邻居内则必 ...

  2. HDU 6116 路径计数

    HDU 6116 路径计数 普通生成函数常用于处理组合问题,指数生成函数常用于处理排列问题. 考虑 对于 $ a $ 个 $ A $ 分为很多堆,这么分的方案数是 $ C_{a-1}^{i-1} $ ...

  3. spring通过注解注册bean的方式+spring生命周期

    spring容器通过注解注册bean的方式 @ComponentScan + 组件标注注解 (@Component/@Service...) @ComponentScan(value = " ...

  4. annovar 注释除人类以外的SNP

    1. 准备文件: ref.fa ref.gtf或者gff3,最好是gtf3,可将gff3转化为gtf sample.vcf 2. 用gff3ToGenePred与gtfToGenePred工具将gtf ...

  5. Metabolomics Workfolw

    推荐R语言界的国内大佬于淼写的代谢组学workflow,包含了大部分代谢组学(以及暴露组)的数据分析方法. Meta-Workflow 主要内容包括: Sample collection Pretre ...

  6. getdelim函数

    利用getdelim函数分割读取字段,将文件制表符替换为空格符 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main( ...

  7. 监督学习&非监督学习

    监督学习 1 - 3 - Supervised Learning  在监督学习中,数据集中的每个例子,算法将预测得到例子的""正确答案"",像房子的价格,或者溜 ...

  8. 02 Windows安装C语言开发工具CodeBlocks

    CodeBlocks安装 使用微信扫码关注微信公众号,并回复:"C语言环境",免费获取下载链接! 1.卸载CodeBlocks(电脑未装此软件,跳过)    进入目录:C:\Pro ...

  9. HMS Core Discovery直播预告 | AI画质增强 ,开启超清视界

    [直播入口] B站华为开发者联盟:http://live.bilibili.com/22551651 4K.8K视频屡见不鲜,HD.FHD分辨率成小屏标配,当网络卡顿.视频自动切换到较低画质时,用户最 ...

  10. Java 数据类型转化

    目录 Java类型转化 基本数据类型自动类型转换 自动类型提升 强制类型转换 - 自动类型提升的逆运算 int与long int类型与String类型 int类型转换成String类型 方法1:+ 拼 ...