py mysql结合面向对象,实现类似超市导购机器的功能【4.商品信息】
py mysql结合面向对象,实现类似超市导购机器的功能(自我总结)
步骤4:
商品信息相关操作实现,主要是各种查询操作
实现代码:
def run(self): while True: print('-----商品信息-----') print('1.查看所有商品信息') print('2.按商品名称搜索') print('3.查看价格最高商品信息') print('4.查看价格最低商品信息') print('5.返回上一级') num = input('请输入操作类型:') if num == '1': # 查看所有商品的信息 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""" count = self.cs1.execute(sql) print('共计搜索到%s条商品信息' % count) goods_info = self.cs1.fetchall() for temp in 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 == '2': # 按商品名称搜索 find_goodsname = input('请输入要搜索的商品名称:') args = '%' + find_goodsname + '%' 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 g.name like '%s';""" % args count = self.cs1.execute(sql) print('共计搜索到%s条 %s相关数据' % (count, find_goodsname)) 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 == '3': # 查看价格最高商品信息 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 g.price=(select max(price) from goods);""" count = self.cs1.execute(sql) print('价格最高商品:') 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': # 查看价格最低商品信息 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 g.price=(select min(price) from goods);""" count = self.cs1.execute(sql) print('价格最低商品:') 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 == '5': break else: print('操作模块选择错误,请重新输入')
----------END

浙公网安备 33010602011771号