PyMySQL设计-进销存系统-配置及输入

发布于:2023-01-17 ⋅ 阅读:(571) ⋅ 点赞:(0)

这系列对新手比较友好,点个赞,收藏一下吧

目录

前缀

实现

总目录


前缀

废话不多说,先配置好环境

pip install pymysql -i https://pypi.tuna.tsinghua.edu.cn/simple

安装pymysql库,有关下载源可参考Python笔记:pip下载切换国内下载源_漠北银虎的博客-CSDN博客

当然,有关于pymysql基本操作的教程有很多,推荐一个,这里就直接开始了【Python之pymysql库学习】1.创建数据库(保姆级图文+实现代码)_发现你走远了的博客-CSDN博客_pymysql 创建数据库

实现

本篇先来实现输入货物板块

既然要输入货物,那就得先选择表格

choice = "";
def Main()//与后文一样,都定义在Main中
    def choose(self):
        global choice
        choice = ""
        sql = "show tables from erptest"//数据库名
        cursor.execute(sql)
        results = cursor.fetchall()//获取所有表格信息
        if len(results) >= 1:
            print("能选择操作的表格如下:")
            for i in range(len(results)):
                print(color.green(str(i + 1) + "."),results[i][0])
            chose = input(color.green("您要选择哪个表格?(输入表格名或编号)"))//color是实现彩色的辅助功能,后期会讲到
            if self.judge(chose,isint = True):
                if len(results) >= int(chose):
                    choice = results[int(chose) - 1][0]
                    return choice
            else:
                for row in results:
                    if row[0] == chose:
                        choice = chose
                        return choice
                print(color.red("输入错误请重新输入"))
        else:
            print("当前没有可操作的表格,正在新建")
            self.upload()

在选择好了表格之后便可以输入货物 

def Main(object):

    # plus,bbb,ccc 完成了录入货物的辅助功能 [bbb,ccc为plus的判断条件]
    def plus(self):
        id = input("请输入货品编号:")
        if id.isdigit() and int(id) > 0://判断数据类型,防止报错
            while self.bbb_good(id):
                if self.bbb_good(id):
                    print("该编号已占用")
                id = input("请输入货物编号:")
        else:
            print("编号必须为正整数!")
            id = input("请输入货物编号:")
            while self.judge(id,isint = False) == False or self.bbb_good(id):
                if self.bbb_good(id):
                    print("该编号已占用")
                id = input("请输入货物编号:")
        good_name = input("请输入货物名称:")
        good_price = input("请输入货物单价:")
        while not self.judge(good_price):
            good_price = input("请输入货物单价:")
        good_count = input("请输入货物数量:")
        good_unit = input("输入货物计量单位:")
        while not self.judge(good_count):
            good_count = input("请输入货物数量:")
        good_type = input("请输入货物类型:")
        sql_insert = "insert into " + choice + "(id,name,number,price,unit,type) values(%s,%s,%s,%s,%s,%s)"
        try:
            cursor.execute(sql_insert,(str(id),str(good_name),str(good_count),str(good_price),str(good_unit),str(good_type)))
            conn.commit()//提交
            print("添加货物成功!")
        except Exception as e:
            print(e)
            conn.rollback()//如果失败,回滚
            print('添加失败')

       # 判断条件,判断数据库中有没有要添加的id
    def bbb_good(self,id):
        global choice//choice是当前的数据库中选择的表格
        sql_fetch = "select * from " + choice + " where id >= 1"
        cursor.execute(sql_fetch)
        results = cursor.fetchall()
        for item in results:
            if str(item[0]) == id:
                return True
        return False

    # 判断条件,判断输入的格式是否正确
    def judge(self,str,isint = False):
        if isint:
            try:
                res = int(str)
            except:
                print(color.red("您输入的数据格式为非整数"))
                return False
            else:
                return True
        else:
            try:
                res = float(str)
            except:
                print(color.red("您输入的数据格式为非小数"))
                return False
            else:
                return True

嘿,别走哇,点个赞吧。文章会持续更新哒!

总目录

PyMySQL设计-进销存系统-多彩美化 删除表格 创建表格_琉璃果子的博客-CSDN博客

PyMySQL设计-进销存系统-配置及输入_琉璃果子的博客-CSDN博客
PyMySQL设计-进销存系统-查看_琉璃果子的博客-CSDN博客
PyMySQL设计-进销存系统-删除,修改货物~设置_琉璃果子的博客-CSDN博客

PyMySQL设计-进销存系统-完整版-_琉璃果子的博客-CSDN博客