python使用笔记14--商品管理小练习

  1 import json
  2 import pymysql
  3 IP = '127.0.0.1'
  4 PORT = 3306
  5 USER_NAME = 'root'
  6 PASSWORD = '123456'
  7 DB = 'db001'
  8 def connect_mysql():#创建mysql连接
  9     connect = pymysql.connect(host=IP,
 10                               port=PORT,
 11                               user=USER_NAME,
 12                               password=PASSWORD,
 13                               db=DB,
 14                               charset='utf8',
 15                               autocommit=True
 16                               )
 17     return connect
 18 
 19 def select_all_products(pro_name=None):#查询数据,商品名称为空,查询所有数据;商品名称不为空,查询指定商品数据
 20     connect = connect_mysql()
 21     cur = connect.cursor(pymysql.cursors.DictCursor)#建立游标
 22     select_sql = "select * from tb_product "
 23     if pro_name:
 24         select_sql += "where pro_name = '%s' ;"%pro_name
 25     else:
 26         select_sql += ";"
 27     cur.execute(select_sql)
 28     result = cur.fetchall()
 29     cur.close()
 30     connect.close()
 31     return result
 32 
 33 
 34 def insert_product(pro_name,price,count,color):#新增商品信息
 35     connect = connect_mysql()
 36     cur = connect.cursor(pymysql.cursors.DictCursor)  # 建立游标
 37     insert_sql = "insert into tb_product(pro_name,price,count,color) VALUES ('%s',%f,%d,'%s') ;"
 38                  %(pro_name,price,count,color)
 39     print(insert_sql)
 40     cur.execute(insert_sql)
 41     result = cur.fetchall()
 42     cur.close()
 43     connect.close()
 44 
 45 def update_product(pro_name,price,count,color):#修改商品信息
 46     connect = connect_mysql()
 47     cur = connect.cursor(pymysql.cursors.DictCursor)  # 建立游标
 48     update_sql = "update tb_product set price = %f , count = %d, color = '%s' where pro_name ='%s' ;" 
 49                  % (price, count, color, pro_name)
 50     print(update_sql)
 51     cur.execute(update_sql)
 52     result = cur.fetchall()
 53     cur.close()
 54     connect.close()
 55 
 56 def delete_product(pro_name):#删除商品信息
 57     connect = connect_mysql()
 58     cur = connect.cursor(pymysql.cursors.DictCursor)  # 建立游标
 59     delete_sql = "DELETE FROM tb_product where pro_name = '%s' ;"%pro_name
 60     print(delete_sql)
 61     cur.execute(delete_sql)
 62     result = cur.fetchall()
 63     cur.close()
 64     connect.close()
 65 
 66 def get_product_name():
 67     for i in range(3):
 68         name = input("请输入商品名称:").strip()
 69         if name:
 70             return name
 71         else:
 72             print('商品名称不能为空')
 73     else:
 74         quit("错误次数过多")
 75 
 76 def show():
 77     name = get_product_name()
 78     if name == 'all':
 79         print(select_all_products())
 80     elif select_all_products(name):
 81         print("商品信息是%s"%select_all_products(name))
 82     else:
 83         print('商品不存在!')
 84 
 85 def delete():
 86     name = get_product_name()
 87     if select_all_products(name):
 88         delete_product(name)
 89         print("商品已经被删除")
 90     else:
 91         print('商品不存在!')
 92 
 93 def check_count(count:str):
 94     if count.isdigit():
 95         if int(count)>0:
 96             return int(count) #1
 97     #None
 98 
 99 def check_price(price:str):
100     count = check_count(price)
101     if count:
102         return count
103     else:
104         if price.count('.')==1 and price.replace('.','').isdigit():
105             return float(price) if float(price)>0 else None
106 
107 def add():
108     name = get_product_name()
109     if select_all_products(name):
110         print('无法添加')
111     else:
112         price, count, color = input_product()
113         if price and count and color:
114             insert_product(name, price, count, color)
115             print("添加成功!")
116         else:
117             print("价格/数量/颜色不合法")
118 
119 
120 def modify():
121     name = get_product_name()
122     if select_all_products(name):  # 商品存在可以修改
123         price, count, color = input_product()
124         if price and count and color:
125             update_product(name, price, count, color)
126             print("修改成功!")
127         else:
128             print("价格/数量/颜色不合法")
129     else:
130         print('商品不存在!')
131 
132 
133 def input_product():
134     price = input("price:").strip()
135     count = input("count:").strip()
136     color = input("color:").strip()
137     price = check_price(price)
138     count = check_count(count)
139     return price,count,color
140 
141 
142 choice = input("请输入:1、添加2、修改、3、查看4、删除、other、退出:").strip()
143 func_map = {'1':add,'2':modify,'3':show,'4':delete}
144 if choice in func_map:
145     func_map.get(choice)()
146 else:
147     quit("退出程序!")
原文地址:https://www.cnblogs.com/cjxxl1213/p/12905041.html