python 银行管理系统

这是一个使用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()
posted @ 2021-12-01 11:05  supermao12  阅读(915)  评论(0编辑  收藏  举报