这是一个使用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. MetaboAnalyst的多组学分析

    MetaboAnalyst是做代谢的R包,功能十分强大.也开发了web版本,代谢组学的分析这里不介绍,主要讲讲它开发的多组学分析的相关内容. 既然是做代谢的工具,即使是增加了多组学内容,肯定也是以代谢 ...

  2. BSA分析

    目录 两种算法 1. 欧氏距离(ED)算法 2. SNP-index算法 实操 1. 上游分析 2. 下游分析 两种算法 1. 欧氏距离(ED)算法 mut与wt分别代表突变型混池.野生型混池,A.C ...

  3. Linux—yum的python版本错误——初级解决方案

    为了安装rrdtool,发现不是少这个就是少那个,最后发现yum也不能用. 从网上找的解决yum问题. 转自:http://doarthon.blog.51cto.com/3175384/728809 ...

  4. oracle中char],varchar,varchar2

    VARCHAR.VARCHAR2.CHAR的区别 1.CHAR的长度是固定的,而VARCHAR2的长度是可以变化的, 比如,存储字符串"abc",对于CHAR (20),表示你存储 ...

  5. javaSE高级篇2 — 流技术 — 更新完毕

    1.先认识一个类----File类 前言:IO相关的一些常识 I / O----输入输出 I     输入     input 0    输出     output I / o 按数据的流动方向来分- ...

  6. Android editttext只能输入不能删除(选中后被软键盘遮住)

    感谢https://www.dutycode.com/post-20.html: 解决方法:在布局外外嵌一层scrollview.

  7. 云原生时代,为什么基础设施即代码(IaC)是开发者体验的核心?

    作者 | 林俊(万念) 来源 |尔达 Erda 公众号 从一个小故事开始 你是一个高级开发工程师. 某天,你自信地写好了自动煮咖啡功能的代码,并在本地调试通过.代码合并入主干分支后,你准备把服务发布到 ...

  8. acre, across

    acre The acre is a unit of land area used in the imperial and US customary systems. It is traditiona ...

  9. Hadoop org.apache.hadoop.util.DiskChecker$DiskErrorException问题等价解决linux磁盘不足解决问题排查

    org.apache.hadoop.util.DiskChecker$DiskErrorException问题等价解决linux磁盘不足解决问题排查 解决"/dev/mapper/cento ...

  10. MapReduce的类型与格式

    MapReduce的类型 默认的MR作业 默认的mapper是Mapper类,它将输入的键和值原封不动地写到输出中 默认的partitioner是HashPartitioner,它对每条记录的键进行哈 ...