模板类型表的增删改查
import pymysqlSQL_Select = "SELECT * FROM alteration_type WHERE id = %d"SQL_Insert = """INSERT INTO alteration_type(create_time,update_time,id,name,type_level,father)VALUES(NOW(),NOW(),%d,'%s',%d,%d)"""SQL_Update = "UPDATE alteration_type SET update_time=NOW(), name='%s',type_level=%d,father=%d where id=%d"SQL_Delete = " DELETE FROM alteration_type WHERE id = %d "SQL_TypeLevel = "SELECT type_level FROM alteration_type where name = '%s'; "SQL_Id = "SELECT id FROM alteration_type where name = '%s'; "# 嵌套查询,找出子节点father号为该id的条目。SQL_IdSon = "SELECT id FROM alteration_type WHERE father = (SELECT id FROM alteration_type where name = '%s');"SQL_IdGrandSon = "SELECT id FROM alteration_type WHERE father = %d;"def main():# 连接数据库# UpdateData("LianHu", 3, 107, 108)# InsertData(101,'北京', 1, 1)# InsertData(102,'深圳', 1, 1)# InsertData(103,'陕西 ', 1, 1)# InsertData(104,'朝阳区', 2, 101)# InsertData(105,'海淀区', 2, 101)# InsertData(106,'宝安区', 2, 102)# InsertData(107,'南山区', 2, 102)# InsertData(108,'西安', 2, 103)# InsertData(109,'莲湖区', 3, 108)# InsertData(110,'蓝田县', 3, 108)# InsertData(111,'周至县', 3, 108)# InsertData(112,'咸阳', 2, 103)# InsertData(113,'秦都区', 3, 112)#DeleteData('西安')#SelectData('蓝田县')# SelectData('西安')#SelectData('陕西')def DB():db = pymysql.connect(host='127.0.0.1', user='root', password='redhat',db='bdp', port=3506, charset='utf8')return dbdef InsertData(id,name, type_level, father): # okdb = DB()cursor = db.cursor()try:cursor.execute(SQL_Insert %(id,name,type_level,father))db.commit()except:print("Insert Error!")db.rollback()db.close()def SelectData(name):db = DB()# 创建游标,创建一个cursor执行一条SQLcursor = db.cursor()cursor.execute(SQL_TypeLevel % name)type_level = cursor.fetchone()[0]cursor.execute(SQL_Id % name)id = cursor.fetchone()[0]id_son=[]cursor.execute(SQL_IdSon % name)for i in range(cursor.rowcount):id_son.append(cursor.fetchone()[0])id_grandson=[]for i in range(len(id_son)):cursor.execute(SQL_IdGrandSon % id_son[i])for j in range(cursor.rowcount):id_grandson.append(cursor.fetchone()[0])#try: # 判断typelevel,1、2、3。再进行删除if type_level == 1:res_grand=[]for i in id_grandson:cursor.execute(SQL_Select %i)res_grand.append(cursor.fetchone())res_son=[]for j in id_son:cursor.execute(SQL_Select %j)res_son.append(cursor.fetchone())res_self=[]cursor.execute(SQL_Select % id)res_self=cursor.fetchone()res=[]res.append(res_self)res.append(res_son)res.append(res_grand)print(res)elif type_level == 2:res_son=[]res_self=[]res=[]for i in id_son:cursor.execute(SQL_Select % i)res_son.append(cursor.fetchone())cursor.execute(SQL_Select % id)res_self=cursor.fetchall()res.append(res_self)res.append(res_son)print(res)else:res_self=[]cursor.execute(SQL_Select % id)res_self=cursor.fetchall()print(res_self)#except:# print("Select Error!")db.close()def UpdateData(name,type_level,father,id): # okdb = DB()cursor = db.cursor()try:cursor.execute(SQL_Update % (name ,type_level,father,id))db.commit()except:print("Update Error!")db.close()def DeleteData(name): # okdb = DB()cursor = db.cursor()cursor.execute(SQL_TypeLevel % name)type_level = cursor.fetchone()[0]cursor.execute(SQL_Id % name)id = cursor.fetchone()[0]id_son=[]cursor.execute(SQL_IdSon % name)for i in range(cursor.rowcount):id_son.append(cursor.fetchone()[0])id_grandson=[]for i in range(len(id_son)):cursor.execute(SQL_IdGrandSon % id_son[i])for j in range(cursor.rowcount):id_grandson.append(cursor.fetchone()[0])# 判断typelevel,1、2、3。再进行删除if type_level == 1:for i in id_grandson:cursor.execute(SQL_Delete %i)for j in id_son:cursor.execute(SQL_Delete %j)cursor.execute(SQL_Delete % id)elif type_level == 2:cursor.execute(SQL_Delete % id_son)cursor.execute(SQL_Delete % id)else:cursor.execute(SQL_Delete % id)db.commit()db.close()if __name__ == "__main__":main()
