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

python使用笔记14--商品管理小练习的更多相关文章

  1. Linux实战教学笔记14:用户管理初级(下)

    第十四节 用户管理初级(下) 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,用户查询相关命令id,finger,users,w,who,last,lastlog,gr ...

  2. Linux实战教学笔记14:用户管理初级(上)

    第十四节 用户管理初级(上) 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,账号管理 1.1 管理用户命令汇总 命令 注释说明(特殊颜色的必须掌握) useradd增 ...

  3. Python入门笔记(14):Python的字符编码

    一.字符编码中ASCII.Unicode和UTF-8的区别 点击阅读:http://www.cnblogs.com/kingstarspe/p/ASCII.html 再推荐一篇相关博文:http:// ...

  4. python学习笔记14(多态、封装、继承)

    创建自已的对象(尤其是类型或者被称为类的对象)是python非常核心的概念. 多态: 可对不同类的对象使用同样的操作. 封装:对外部世界隐藏对象的工作细节. 继承:以普通的类为基础建立专门的类对象. ...

  5. Python学习笔记14—模块

    在python中所有的模块都被加入到了sys.path中,用下面的方法可以看见模块的位置. >>> import sys >>> import pprint > ...

  6. Python学习笔记14:标准库之信号量(signal包)

    signal包负责在Python程序内部处理信号.典型的操作包含预设信号处理函数,暂停并等待信号,以及定时发出SIGALRM等. 要注意,signal包主要是针对UNIX平台(比方Linux, MAC ...

  7. python学习笔记(14):可视化分析

    一.Matplotlib 1.用于创建出版质量图表的绘图工具库 2.目的的为Python构建一个Matlab式的绘图接口 3.import matplotlib.pyplot as plt:pyplo ...

  8. Python使用笔记20--网络操作小练习

    1 ''' 2 2.自己抓取qq群的接口,传入一个群号,然后把群成员的头像下载到本地,头像用群备注来命名,如果没有 3 群备注,那么取昵称. 4 ''' 5 import requests 6 imp ...

  9. python使用笔记13--清理日志小练习

    1 ''' 2 写一个删除日志的程序,删除5天前或为空的日志,不包括当天的 3 1.删除5天前的日志文件 4 2.删除为空的日志文件 5 ''' 6 import os 7 import time 8 ...

随机推荐

  1. xss-代码角度理解与绕过filter

    0x00 原理   xss全称为cross site scripting,中文为跨站脚本攻击.它允许web用户将恶意代码植入到提供给用户使用的页面.代码包括HTML代码和客户端脚本. 0x01 危害 ...

  2. linux ( crontab 定时任务命令)

    linux ( crontab 定时任务命令)    crontab 定时任务命令 linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工 ...

  3. Go基础结构与类型06---房贷计算器

    package main import ( "fmt" "math" "strconv" ) /* 输入的金额.年化利息(0.05代表5%) ...

  4. 状压dp(总结)状态压缩

    状压这个和二进制分不开关系 所以,对于二进制的熟悉是必不可少的技能 &  与操作,1不变,0变0 |  或操作,0不变,1变1 ^  异或操作,0不变,1取反 - 取反操作,把每一个二进制位0 ...

  5. 图像实例分割:CenterMask

    图像实例分割:CenterMask CenterMask: single shot instance segmentation with point representation 论文链家: http ...

  6. 旷视MegEngine基本概念

    旷视MegEngine基本概念 MegEngine 是基于计算图的深度神经网络学习框架. 本文简要介绍计算图及其相关基本概念,以及它们在 MegEngine 中的实现. 计算图(Computation ...

  7. 多级中间表示概述MLIR

    多级中间表示概述MLIR MLIR项目是一种构建可重用和可扩展的编译器基础结构的新颖方法.MLIR旨在解决软件碎片,改善异构硬件的编译,显着降低构建特定于域的编译器的成本以及帮助将现有编译器连接在一起 ...

  8. Python_selenium页面元素整合设计经验

  9. Collection&Map

    1.Collection 添加元素 boolean add(E e) 删除元素 boolean remove(E e) 元素个数 int size() 清空 void clear() 判空 boole ...

  10. JavaFx 创建快捷方式及设置开机启动

    原文地址:JavaFx 创建快捷方式及设置开机启动 | Stars-One的杂货小窝 原本是想整个桌面启动器,需要在windows平台上实现开机启动,但我的软件都是jar文件,不是传统的exe文件,也 ...