import pymysql
class Unlimited:
# 连接数据库
def __init__(self):
self.db = pymysql.connect('localhost', 'root', '******', 'test')
self.db.set_charset('utf8')
self.cursor = self.db.cursor()
# 菜单函数
def menu(self):
while True:
print('******************************************\n')
print('1.添加顶级分类**\n')
print('2.添加子分类****\n')
print('3.查询********\n')
print('4.删除********\n')
print('5.退出********\n')
print('*****************************************\n')
state = input('请输入要执行的操作:')
if state == '1':
self.addTopCategory()
elif state == '2':
self.addSonCategory()
elif state == '3':
self.showData()
elif state == '4':
self.delete()
elif state == '5':
return
else:
print('输入错误,请重新输入')
self.db.close()
# 添加顶级分类
def addTopCategory(self):
# 输入类别名称
goodsname = input('请输入要添加的类别名称:')
# 查询types表中为输入名称的分类名
nameSql = 'select goodsname from types where goodsname="%s"'
# 用reslut接收下 方便下面判断
result = self.cursor.execute(nameSql % goodsname)
# 如果sql语句作用了0行 那么表示该类别名不存在 可以添加
if result == 0:
# 插入类别
sql = 'insert into types (goodsname, pid, path) values("%s", %d, "%s")'
data = (goodsname, 0, "0,")
self.cursor.execute(sql % data)
self.db.commit()
else:
print('类别已存在')
# 添加子类别函数
def addSonCategory(self):
# 输入你要依附的父级
goodsname = input('请输入父类名称:')
# 查询父级中的一些字段 方便下面使用
nameSql = 'select id,goodsname,path from types where goodsname="%s"'
# 用变量接受 方便下面判断
result = self.cursor.execute(nameSql % goodsname)
# 如果受影响的行不止一行则表示父类别存在 即可进行下列的操作
if result != 0:
# 获取查询的结果
path = self.cursor.fetchone()
sonName = input('请输入子类名称:')
# 插入子类别
insertSon = 'insert into types (goodsname, pid, path) values("%s", %d, "%s");'
data = (sonName, path[0], (path[-1] + str(path[0]) + ','))
self.cursor.execute(insertSon % data)
self.db.commit()
else:
print('父类不存在')
# 展示函数
def showData(self):
# 此与上面几个函数类似
type = input('请输入要查看的类别:')
sql = 'select id, goodsname, path from types where goodsname="%s"'
result = self.cursor.execute(sql % type)
if result != 0:
# 将查询出的结果 取出path 然后用,号分割形成一个列表 然后操作path内的值去查询父级内容
path = self.cursor.fetchone()[-1].split(',')
mySelf = path[0]
try:
# 遍历path中的内容 去掉头尾 因为头尾不是id 逐条查询并输出
for i in range(len(path)):
if i != 0 and i != (len(path)-1):
a = int(path[i])
resultSql = 'select goodsname from types where id=%d'
self.cursor.execute(resultSql % a)
print('*' * i, end='')
print(self.cursor.fetchone()[0])
# 在循环内无法打印自己 因为path中不包含自身的id 所以在循环外 也就是最后打印
print('*' * i, end='')
print(type)
except:
return
else:
print('类别不存在')
# 删除函数
def delete(self):
# 前面部分与上面相同
type = input('请输入要删除的类别:')
sql = 'select id from types where goodsname="%s"'
result = self.cursor.execute(sql % type)
if result != 0:
nowId = str(self.cursor.fetchone()[0])
# 查询出所有的path
pathSql = 'select path from types'
self.cursor.execute(pathSql)
resultTuple = self.cursor.fetchall()
# 将path遍历
for i in resultTuple:
# print(i)
# 如果当前分类的id在其他的path内 即当前分类为父级 不能删除
for x in i:
if nowId in x:
print('该级别是父级,不能被删除')
return
# 删除语句
delSql = 'delete from types where id=%d' % int(nowId)
self.cursor.execute(delSql)
self.db.commit()
print('删除成功')
else:
print('该类别不存在')
myUnlimited = Unlimited()
myUnlimited.menu()
python学习之无限级分类
最新推荐文章于 2022-07-10 21:10:57 发布