py mysql结合面向对象,实现类似超市导购机器的功能【6.品牌信息】

py mysql结合面向对象,实现类似超市导购机器的功能(自我总结)

步骤6:

商品品牌相关操作实现,主要是各种查询操作

实现代码:

    def run(self):
        while True:
            print('-----商品品牌-----')
            print('1.查看所有商品品牌')
            print('2.查看每个商品品牌下的所有商品信息')
            print('3.根据商品品牌查询商品信息')
            print('4.返回上一级')
            num = input('请输入操作类型:')
            if num == '1':
                # 查看所有商品品牌
                sql = """select * from goods_brands"""
                count = self.cs1.execute(sql)
                print('共计搜索到%s条商品品牌信息' % count)
                goods_brands_info = self.cs1.fetchall()
                for temp in goods_brands_info:
                    t_len = len(temp)
                    for i in range(t_len):
                        if i == t_len - 1:
                            print('品牌:%s' % temp[i])
                        else:
                            print('BrandID:%d' % temp[i], end=',')

            elif num == '2':
                # 查看每种商品品牌下的所有商品信息
                sql = """select b.name,group_concat(g.name,' ',c.name,' ',price) 
                            from goods as g ,goods_cates as c,goods_brands as b 
                            where g.cate_id=c.id and g.brand_id=b.id group by b.id;"""
                count = self.cs1.execute(sql)
                print('共计搜索到%s个商品品牌' % count)
                brands_goods = self.cs1.fetchall()
                for temp in brands_goods:
                    t_len = len(temp)
                    for i in range(t_len):
                        if i == t_len - 1:
                            print(temp[i])
                        else:
                            print(temp[i], end=':')

            elif num == '3':
                # 根据商品品牌查询商品信息
                find_brand = input('请输入要查询的商品品牌名称:')
                args = '%' + find_brand + '%'
                sql = """select g.name,c.name,b.name,g.price from goods as g
                                    inner join goods_cates as c on g.cate_id=c.id 
                                    inner join goods_brands as b on g.brand_id=b.id 
                                    having b.name like %s;"""
                count = self.cs1.execute(sql, args)
                print('共计搜索到%s品牌下%s个商品信息' % (find_brand, count))
                find_goods_info = self.cs1.fetchall()
                for temp in find_goods_info:
                    t_len = len(temp)
                    for i in range(t_len):
                        if i == t_len - 1:
                            print(temp[i], '')
                        else:
                            print(temp[i], end='')

            elif num == '4':
                # 返回上一级
                break
            else:
                print('操作模块选择错误,请重新输入')

 

posted @ 2020-08-14 15:40  zhou&zhou  阅读(176)  评论(0)    收藏  举报