第十节 python操作sql查询数据
1 import pymysql 2 3 ''' 4 print(cursor.fetchone()) 5 # 有点类似next,前面取过了后面就调用就不会从头开始取了 6 print(cursor.fetchmany(2)) 7 print(cursor.fetchall()) 8 conn = pymysql.connect(host='localhost', port='3306', user='root', password='', database='python_test', charset='utf8') 9 ''' 10 class JD(): 11 12 def __init__(self): 13 self.conn = pymysql.connect('localhost','root','','python_test') 14 self.cursor = self.conn.cursor() 15 # cursor.close() 16 # conn.close() 17 # cursor.execute('select * from tdb_goods') 18 19 def sql_exe(self,sql): 20 self.cursor.execute(sql) 21 ret = self.cursor.fetchall() 22 return ret 23 24 def show_all_item(self): 25 for temp in self.sql_exe('select * from tdb_goods'): 26 print(temp) 27 28 def show_goods_cate(self): 29 for temp in self.sql_exe('select * from goods_cate'): 30 print(temp) 31 32 def show_brand_name(self): 33 for temp in self.sql_exe('select * from brand_name'): 34 print(temp) 35 36 @staticmethod 37 def mue(): 38 print("......京东商城......") 39 print('1:所有的商品') 40 print('2:所有商品的分类') 41 print('3:所有的商品品牌分类') 42 print('0:关闭商城') 43 return input('请输入功能相对于的序号:') 44 45 def run(self): 46 while True: 47 num = self.mue() 48 if num == '1': 49 self.show_all_item() 50 elif num == '2': 51 self.show_goods_cate() 52 elif num == '3': 53 self.show_brand_name() 54 elif num == '0': 55 break 56 else: 57 print('输入有误,请重新输入....') 58 self.cursor.close() 59 self.conn.close() 60 61 62 def main(): 63 jd = JD() 64 jd.run() 65 66 67 if __name__ == '__main__': 68 main()