rom pymysql import connect
class TCL (object):
def __init__(self):
#創(chuàng)建Connection連接
self.conn = connect(host = 'localhost',port = 3306,user = 'root' ,password = 'hezhuang',database = 'TCL',charset = 'utf8')
#? 獲得Cursor對象
self.cursor = conn.cursor()
def __del__(self):
#關閉Cursor對象
self.cursor.close()
self.conn.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_cates;'
self.execute_sql(sql)
def show_brands(self):
sql = 'select name from goods_brands;'
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.conn.commit()
def get_info_by_name(self):
# find_name = input('請輸入查詢的名字')
# sql = '''select * from goods where name = "%s";'''%find_name
# print('--->%s<---'%sql)
# self.execute_sql(sql)
#上面注釋的代碼有可能導致那槽。? sql注入?? 預防如下代碼
sql = 'select * from goods where name = %s'
self.cursor.execute(sql,[find_name])#防止sql注入
print(self.cursor.fetchall())
@staticmethod
def print_menu(self):
print('---------TCL--------')
print('1:所有商品')
print('2:所有的商品分類')
print('3:所有商品品牌分類')
print('4:添加商品分類')
print('5:刪除商品分類')
print('6:修改商品分類')
print('7:根據(jù)名字查詢商品')
return input('請輸入功能對應的序號')
def run(self):
while? True:
num = self.print_menu()
if num == '1':
#查詢所有商品
self.show_all_items()
elif num == ‘2’:
#查詢商品分類
self.show_cares()
elif num == '3':
#查詢商品品牌分類
self.show_brands()
elif num == '4':
#添加商品品牌分類
self.add_cates()
elif num == '5':
#刪除商品品牌分類
pass
elif num == '6':
#修改商品品牌分類
pass
elif num == '7':
#根據(jù)名字查詢商品
self.get_info_by_name()
else:
print('輸入有誤,請重新輸入')
def main():
#1 創(chuàng)建TCL商城對象
tcl = TCL()
#2 調用這個對象run方法奴曙,并且使它運行
tcl.run()
if __name__ == '_main_:
main()
#刪的方案。方案一 (——真刪——)陈惰。將所有分類下商品的類別置為NULL。再刪除分類
#方案二佣盒。 (---假刪---) 添加是否顯示該分類的字段is_show? 更新該字段為隱藏
#SQL注入
# ' or 1=1 or' 1