1. 程式人生 > >python第一次連接mysql數據庫

python第一次連接mysql數據庫

where png change 關閉 http 查詢 分享 src count

文件結構圖

技術分享圖片

源碼

jing_dong.py

 1 # coding:utf-8
 2 from mysql_connect import MySql
 3 
 4 
 5 class JD(object):
 6     def __init__(self):
 7         self.mysql = MySql(localhost, 3306, root, ‘‘, ksoa_tdyy_test, utf8)
 8 
 9     def show_items_info(self):
10         """查詢商品信息"""
11         item_name = input("
請輸入要查詢的商品名稱:") 12 sql = """select * from spkfk where spmch=%s or tongym=%s""" 13 for temp in self.mysql.sql_select_all_data(sql, item_name, item_name): 14 print(temp) 15 16 def show_order_info(self): 17 """查詢訂單信息""" 18 item_name = input("請輸入要查詢的訂單號:")
19 sql = """select * from pf_djhz where djbh=%s""" 20 for temp in self.mysql.sql_select_all_data(sql, item_name): 21 print(temp) 22 23 def show_customer_info(self): 24 """查詢顧客信息""" 25 item_name = input("請輸入要查詢的顧客名稱:") 26 sql = """select * from mchk where dwmch=%s
""" 27 for temp in self.mysql.sql_select_all_data(sql, item_name): 28 print(temp) 29 30 def add_items(self): 31 pass 32 33 # 創建靜態方法 34 @staticmethod 35 def print_menu(): 36 """打印菜單""" 37 print("------京東商城--------") 38 print("1:查詢商品信息") 39 print("2:查詢訂單信息") 40 print("3:查詢顧客信息") 41 print("4.添加商品信息") 42 print("0:退出") 43 return input("請輸入功能對應的序號:") 44 45 def run(self): 46 while True: 47 user_option = self.print_menu() 48 if user_option == "1": 49 # 查詢商品信息 50 self.show_items_info() 51 elif user_option == "2": 52 # 查詢訂單信息 53 self.show_order_info() 54 elif user_option == "3": 55 # 查詢顧客信息 56 self.show_customer_info() 57 elif user_option == "4": 58 # 添加商品信息 59 self.add_items() 60 elif user_option == "0": 61 # 退出 62 print("謝謝使用") 63 break 64 else: 65 print("輸入有誤,請重新輸入") 66 continue 67 68 69 def main(): 70 # 1.創建一個京東商城對象 71 jd = JD() 72 73 # 2.調用這個對象的run()方法,讓其運行 74 jd.run() 75 76 77 if __name__ == "__main__": 78 main()

mysql_connect.py

 1 # coding:utf-8
 2 from pymysql import connect
 3 
 4 
 5 class MySql(object):
 6     not_select_data_info = ("提示,", "沒有查詢到數據")
 7 
 8     def __init__(self, host, port, user, password, database, charset):
 9         # 創建connect連接
10         self.conn = connect(host=host, port=port, user=user, password=password, database=database, charset=charset)
11         # 獲得Cursor對象
12         self.cursor = self.conn.cursor()
13 
14     def __del__(self):
15         # 關閉Cursor對象
16         self.cursor.close()
17         # 關閉connect對象
18         self.conn.close()
19 
20     def sql_select_all_data(self, sql_str, *args):
21         """查詢所有數據,返回元組"""
22         """
23         如果sql裏面需要有用戶輸入的參數傳入,這時不要自己手工拼接字符串
24         以元組的方式將多個參數(*args)和sql語句傳給execute,讓其自己拼接
25         用來防止SQL註入
26         """
27         if self.cursor.execute(sql_str, args) != 0:
28             # 返回全部數據
29             return self.cursor.fetchall()
30         # 返回提示信息
31         return self.not_select_data_info
32 
33     def sql_select_one_data(self, sql_str, *args):
34         """查詢一行數據,返回元組"""
35         if self.cursor.execute(sql_str, args) != 0:
36             # 返回全部數據
37             return self.cursor.fetchone()
38         # 返回提示信息
39         return self.not_select_data_info
40 
41     def sql_change_data(self, sql_str, *args):
42         """數據的增、刪、改操作,返回影響的行數"""
43         change_count = self.cursor.execute(sql_str, args)
44         if change_count != 0:
45             # 返回影響的行數
46             return change_count
47         # 返回0
48         return 0

運行效果圖

技術分享圖片

未對查詢出來的數據做顯示優化處理。

python第一次連接mysql數據庫