python第一次连接mysql数据库

文件结构图

源码

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

运行效果图

未对查询出来的数据做显示优化处理。

猜你喜欢

转载自www.cnblogs.com/dave555/p/9718911.html