from pymysql import *
class JD(object):
def init(self):
# 创建connection链接
self.conn = connect(host=‘127.0.0.1’,port=8080, usser=‘root’, password=‘smoy’, database=‘jianda’, charset=‘utf8’)
# 获得cursor对象
self.cursor = self.conn.cursor()
def __del__(self):
# 关闭cursor对象, 当程序结束 Python解释器自动调用此方法
self.cursor.close()
self.conn.close()
def execute_sql(self, sql):
self.cursor.execute(sql)
fro temp in self.cursor.fetchall():
print(temp)
def show_all_items(self):
sql = "select * from goods;"
self.execute_sql(sql)
def show_cates(self):
sql = "select name from goods_brands;"
self.execute_sql(sql)
def show_brands(self):
sql = "select name from goods_brands;"
self.execute_sql(sql)
def add_brands(self):
item_name = input("输入商品分类的名称:")
sql = """insert into goods_brands (name) values ("%s")""" % item_name
self.cursor.execute(sql)
sql.conn.commit()
def get_info_by_anme(self):
find_name = input("请输入要查询的商品的名字")
sql = "select * form goods where name=%s" % find_name
self.cursor.execute(sql, [find_name])
print(self.cursor.fetchall())
@staticmethod
def print_num():
print("1:所有的商品")
print("2:所有的商品分类")
print("3:所有的商品品牌分类")
print("4:添加一个商品分类")
print("5::根据名字查询一个商品")
num = input("请输入功能对应的序号")
return num
def run(self):
while True:
num = JD.print_num()
if num == 1:
# 查询所有商品
self.show_all_items()
elif num == "2":
# 查询分类
self.show_cates()
elif num == "3":
# 商品品牌分类
self.show_brands()
elif num == "4":
# 添加品牌分类
slef.add_brands()
elif num == "5":
# 根据名字查询商品
self.get_info_by_name()
else:
print("输入有误,重新输入..")
def main():
jd = JD()
jd.run()
if name == “main”:
main()