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("退出程序!")
python使用笔记14--商品管理小练习的更多相关文章
- Linux实战教学笔记14:用户管理初级(下)
第十四节 用户管理初级(下) 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,用户查询相关命令id,finger,users,w,who,last,lastlog,gr ...
- Linux实战教学笔记14:用户管理初级(上)
第十四节 用户管理初级(上) 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,账号管理 1.1 管理用户命令汇总 命令 注释说明(特殊颜色的必须掌握) useradd增 ...
- Python入门笔记(14):Python的字符编码
一.字符编码中ASCII.Unicode和UTF-8的区别 点击阅读:http://www.cnblogs.com/kingstarspe/p/ASCII.html 再推荐一篇相关博文:http:// ...
- python学习笔记14(多态、封装、继承)
创建自已的对象(尤其是类型或者被称为类的对象)是python非常核心的概念. 多态: 可对不同类的对象使用同样的操作. 封装:对外部世界隐藏对象的工作细节. 继承:以普通的类为基础建立专门的类对象. ...
- Python学习笔记14—模块
在python中所有的模块都被加入到了sys.path中,用下面的方法可以看见模块的位置. >>> import sys >>> import pprint > ...
- Python学习笔记14:标准库之信号量(signal包)
signal包负责在Python程序内部处理信号.典型的操作包含预设信号处理函数,暂停并等待信号,以及定时发出SIGALRM等. 要注意,signal包主要是针对UNIX平台(比方Linux, MAC ...
- python学习笔记(14):可视化分析
一.Matplotlib 1.用于创建出版质量图表的绘图工具库 2.目的的为Python构建一个Matlab式的绘图接口 3.import matplotlib.pyplot as plt:pyplo ...
- Python使用笔记20--网络操作小练习
1 ''' 2 2.自己抓取qq群的接口,传入一个群号,然后把群成员的头像下载到本地,头像用群备注来命名,如果没有 3 群备注,那么取昵称. 4 ''' 5 import requests 6 imp ...
- python使用笔记13--清理日志小练习
1 ''' 2 写一个删除日志的程序,删除5天前或为空的日志,不包括当天的 3 1.删除5天前的日志文件 4 2.删除为空的日志文件 5 ''' 6 import os 7 import time 8 ...
随机推荐
- xss-代码角度理解与绕过filter
0x00 原理 xss全称为cross site scripting,中文为跨站脚本攻击.它允许web用户将恶意代码植入到提供给用户使用的页面.代码包括HTML代码和客户端脚本. 0x01 危害 ...
- linux ( crontab 定时任务命令)
linux ( crontab 定时任务命令) crontab 定时任务命令 linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工 ...
- Go基础结构与类型06---房贷计算器
package main import ( "fmt" "math" "strconv" ) /* 输入的金额.年化利息(0.05代表5%) ...
- 状压dp(总结)状态压缩
状压这个和二进制分不开关系 所以,对于二进制的熟悉是必不可少的技能 & 与操作,1不变,0变0 | 或操作,0不变,1变1 ^ 异或操作,0不变,1取反 - 取反操作,把每一个二进制位0 ...
- 图像实例分割:CenterMask
图像实例分割:CenterMask CenterMask: single shot instance segmentation with point representation 论文链家: http ...
- 旷视MegEngine基本概念
旷视MegEngine基本概念 MegEngine 是基于计算图的深度神经网络学习框架. 本文简要介绍计算图及其相关基本概念,以及它们在 MegEngine 中的实现. 计算图(Computation ...
- 多级中间表示概述MLIR
多级中间表示概述MLIR MLIR项目是一种构建可重用和可扩展的编译器基础结构的新颖方法.MLIR旨在解决软件碎片,改善异构硬件的编译,显着降低构建特定于域的编译器的成本以及帮助将现有编译器连接在一起 ...
- Python_selenium页面元素整合设计经验
- Collection&Map
1.Collection 添加元素 boolean add(E e) 删除元素 boolean remove(E e) 元素个数 int size() 清空 void clear() 判空 boole ...
- JavaFx 创建快捷方式及设置开机启动
原文地址:JavaFx 创建快捷方式及设置开机启动 | Stars-One的杂货小窝 原本是想整个桌面启动器,需要在windows平台上实现开机启动,但我的软件都是jar文件,不是传统的exe文件,也 ...