第十一节 python增、删、改操作
1 import pymysql 2 3 ''' 4 conn.commit() 真正将数据写入数据库 5 conn.rollback() 取消前面是sql语句操作 6 ''' 7 class JD(): 8 9 def __init__(self): 10 self.conn = pymysql.connect('localhost','root','','python_test') 11 self.cursor = self.conn.cursor() 12 # cursor.close() 13 # conn.close() 14 # cursor.execute('select * from tdb_goods') 15 def sql_exe(self,sql): 16 self.cursor.execute(sql) 17 ret = self.cursor.fetchall() 18 return ret 19 20 def show_all_item(self): 21 for temp in self.sql_exe('select * from tdb_goods'): 22 print(temp) 23 24 def show_goods_cate(self): 25 for temp in self.sql_exe('select * from goods_cate'): 26 print(temp) 27 28 def show_brand_name(self): 29 for temp in self.sql_exe('select * from brand_name'): 30 print(temp) 31 32 def add_brand_name(self): 33 brandname = input('请输入你要添加的品牌名称:') 34 sql = """insert into brand_name (name) values ("%s")""" % brandname 35 self.cursor.execute(sql) 36 self.conn.commit() 37 38 @staticmethod 39 def mue(): 40 print("......京东商城......") 41 print('1:所有的商品') 42 print('2:所有商品的分类') 43 print('3:所有的商品品牌分类') 44 print('4:添加商品品牌') 45 print('0:关闭商城') 46 return input('请输入功能相对于的序号:') 47 48 def run(self): 49 while True: 50 num = self.mue() 51 if num == '1': 52 self.show_all_item() 53 elif num == '2': 54 self.show_goods_cate() 55 elif num == '3': 56 self.show_brand_name() 57 elif num == '0': 58 break 59 elif num == '4': 60 self.add_brand_name() 61 else: 62 print('输入有误,请重新输入....') 63 self.cursor.close() 64 self.conn.close() 65 66 67 def main(): 68 jd = JD() 69 jd.run()