mysql 无限极分类

 1 import pymysql
 2 class Type:
 3     def __init__(self):
 4         # 连接数据库
 5         self.bankData = pymysql.connect("127.0.0.1", "root", "123123", "user")
 6         # 设置字符编码
 7         self.bankData.set_charset('utf8')
 8         # 创建游标对象
 9         self.cursor = self.bankData.cursor()
10 
11     # 添加首类别
12     def addTop(self):
13         typename = input('请输入首类别')
14         # 查找goods是否存在
15         sql = 'select goods from classify where goods= "{}"'.format(typename)
16         self.cursor.execute(sql)
17         inNot = self.cursor.rowcount
18         # 如果存在则return
19         if inNot != 0:
20             print("此首类已存在")
21             sql = 'select * from classify;'
22             self.cursor.execute(sql)
23             for i in self.cursor.fetchall():
24                 print(i)
25             return False
26         # 如果不存在则 try尝试加入数据
27         try:
28             sql = 'insert into classify(id, goods) values(null,"{}")'.format(typename)
29             self.cursor.execute(sql)
30             self.bankData.commit()
31             print("插入成功")
32         except:
33             print("插入失败")
34 
35     # 添加次类别
36     def addSub(self):
37         top = input('请输入首类别')
38         # 查找goods是否存在
39         sql = 'select * from classify where goods= "{}"'.format(top)
40         self.cursor.execute(sql)
41         inNot = self.cursor.rowcount
42         # 如果存在则
43         if inNot != 0:
44             print("此首类已存在")
45             asd = self.cursor.fetchone()
46             zi = input('请输入子类别')
47             str(zi)
48             # try尝试加入数据
49             try:
50                 sql = 'insert into classify(id,goods,gid,path) values(null,"{}",{},"{}")'.format(zi, asd[0], str(asd[3]) + str(asd[0]) + ',')
51                 self.cursor.execute(sql)
52                 self.bankData.commit()
53                 print("插入成功")
54             except Exception as e:
55                 print(e ,"插入失败")
56     # 查找
57     def showType(self):
58         # 排序合并后的地址
59         sql = 'select * from classify order by concat(path,id)'
60         self.cursor.execute(sql)
61         res = self.cursor.fetchall()
62         for n in res:
63             # 统计列表有多少个逗号
64             repeat = n[-1].count(',')
65             # 有多少个逗号就多少个
66             asd = repeat * "  -"
67             print(asd, n[0],n[1], n[2], n[-1])
68 
69     # 删除
70     def delType(self):
71         asd = input("输入需要删除的分类")
72         # 查找要删除的信息有没有
73         sql = 'select * from classify where goods="{}"'.format(asd)
74         self.cursor.execute(sql)
75         # 没有的话会是0 有就大于1
76         inNot = self.cursor.rowcount
77         # 如果不是0
78         if inNot != 0:
79             print("a")
80             # 删除这个数据的整行
81             sql = 'delete from classify where goods="{}"'.format(asd)
82             self.cursor.execute(sql)
83             self.bankData.commit()
84 
85 ty = Type()
86 while True:
87     print("1、添加首类别 2、添加子类别 3、查找 4、删除")
88     userinpur = input("请选择")
89     if userinpur == "1":
90         ty.addTop()
91     if userinpur == "2":
92         ty.addSub()
93     if userinpur == "3":
94         ty.showType()
95     if userinpur == "4":
96         ty.delType()

猜你喜欢

转载自www.cnblogs.com/liyaobin/p/10224801.html