from pymysql import connect
class JD(object):
def __init__(self):
#创建connection连接
self.coon = connect(host='localhost',port=3306,user = 'root' ,password = 'liangqing',database = 'dnf',charset = 'utf8')
#获得cursor对象
self.cursor = self.coon.cursor()
def __del__(self):
# 关闭cursor对象
self.cursor.close()
self.coon.close()
def execute_sql(self,sql):
self.cursor.execute(sql)
for 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;"
self.execute_sql(sql)
def add_cates(self):
item_name = input("请输入新商品分类的名称:")
sql = """insert into goods_cates (name) values("%S");""" % item_name
self.cursor.execute(sql)
self.coon.commit()
def get_into_by_name(self):
find_name = input("请输入要查询的商品的名字:")
#sql = """select * from goods where name = "%s";""" % find_name
#print("-->%s<--" % sql)
#self.execute_sql(sql)
sql = "select * from goods where name = %s"
self.cursor.execute(sql,[find_name])
print(self.cursor.fetchall())
@staticemethod
def print_menu(self):
print("-----京东------")
print("1:所有的商品")
print("2:所有的商品分类")
print("3:所有的商品品牌分类")
print("4:添加商品分类")
print("5:删除商品分类")
print("6:修改商品分类")
print("7:根据名字查询商品")
return input("请输入功能对应的序号:")
def run(self):
while True:
num = show.print_menu()
if num == "1"
#查询所以商品
self.show_all_items()
elif num == "2"
#查询分类
pass
elif num == "3"
#查询品牌分类
self.show_brands
elif num == "4"
#添加商品分类
self.add_cates()
elif num == "5":
#删除商品分类
pass
elif num == "6":
#修改商品分类
pass
elif num == "7":
#根据名字查询商品
self.get_info_by_name()
else:
print("输入有误,请重新输入")
def main():
#1、创建一个京东商城对象
jd = JD()
#2、调用这个对象run方法
jd.run()
if __main__ =='__main__'
main()
#删的方案。方案一 (——真删——)。将所有分类下商品的类别置为NULL。再删除分类
#方案二。 (---假删---) 添加是否显示该分类的字段is_show 更新该字段为隐藏
#SQL注入
# ' or 1=1 or' 1