模板类型表的增删改查

    1. import pymysql
    2. SQL_Select = "SELECT * FROM alteration_type WHERE id = %d"
    3. SQL_Insert = """INSERT INTO alteration_type(
    4. create_time,update_time,id,name,type_level,father)
    5. VALUES(NOW(),NOW(),%d,'%s',%d,%d)
    6. """
    7. SQL_Update = "UPDATE alteration_type SET update_time=NOW(), name='%s',type_level=%d,father=%d where id=%d"
    8. SQL_Delete = " DELETE FROM alteration_type WHERE id = %d "
    9. SQL_TypeLevel = "SELECT type_level FROM alteration_type where name = '%s'; "
    10. SQL_Id = "SELECT id FROM alteration_type where name = '%s'; "
    11. # 嵌套查询,找出子节点father号为该id的条目。
    12. SQL_IdSon = "SELECT id FROM alteration_type WHERE father = (SELECT id FROM alteration_type where name = '%s');"
    13. SQL_IdGrandSon = "SELECT id FROM alteration_type WHERE father = %d;"
    14. def main():
    15. # 连接数据库
    16. # UpdateData("LianHu", 3, 107, 108)
    17. # InsertData(101,'北京', 1, 1)
    18. # InsertData(102,'深圳', 1, 1)
    19. # InsertData(103,'陕西 ', 1, 1)
    20. # InsertData(104,'朝阳区', 2, 101)
    21. # InsertData(105,'海淀区', 2, 101)
    22. # InsertData(106,'宝安区', 2, 102)
    23. # InsertData(107,'南山区', 2, 102)
    24. # InsertData(108,'西安', 2, 103)
    25. # InsertData(109,'莲湖区', 3, 108)
    26. # InsertData(110,'蓝田县', 3, 108)
    27. # InsertData(111,'周至县', 3, 108)
    28. # InsertData(112,'咸阳', 2, 103)
    29. # InsertData(113,'秦都区', 3, 112)
    30. #DeleteData('西安')
    31. #SelectData('蓝田县')
    32. # SelectData('西安')
    33. #SelectData('陕西')
    34. def DB():
    35. db = pymysql.connect(host='127.0.0.1', user='root', password='redhat',
    36. db='bdp', port=3506, charset='utf8')
    37. return db
    38. def InsertData(id,name, type_level, father): # ok
    39. db = DB()
    40. cursor = db.cursor()
    41. try:
    42. cursor.execute(SQL_Insert %(id,name,type_level,father))
    43. db.commit()
    44. except:
    45. print("Insert Error!")
    46. db.rollback()
    47. db.close()
    48. def SelectData(name):
    49. db = DB()
    50. # 创建游标,创建一个cursor执行一条SQL
    51. cursor = db.cursor()
    52. cursor.execute(SQL_TypeLevel % name)
    53. type_level = cursor.fetchone()[0]
    54. cursor.execute(SQL_Id % name)
    55. id = cursor.fetchone()[0]
    56. id_son=[]
    57. cursor.execute(SQL_IdSon % name)
    58. for i in range(cursor.rowcount):
    59. id_son.append(cursor.fetchone()[0])
    60. id_grandson=[]
    61. for i in range(len(id_son)):
    62. cursor.execute(SQL_IdGrandSon % id_son[i])
    63. for j in range(cursor.rowcount):
    64. id_grandson.append(cursor.fetchone()[0])
    65. #try: # 判断typelevel,1、2、3。再进行删除
    66. if type_level == 1:
    67. res_grand=[]
    68. for i in id_grandson:
    69. cursor.execute(SQL_Select %i)
    70. res_grand.append(cursor.fetchone())
    71. res_son=[]
    72. for j in id_son:
    73. cursor.execute(SQL_Select %j)
    74. res_son.append(cursor.fetchone())
    75. res_self=[]
    76. cursor.execute(SQL_Select % id)
    77. res_self=cursor.fetchone()
    78. res=[]
    79. res.append(res_self)
    80. res.append(res_son)
    81. res.append(res_grand)
    82. print(res)
    83. elif type_level == 2:
    84. res_son=[]
    85. res_self=[]
    86. res=[]
    87. for i in id_son:
    88. cursor.execute(SQL_Select % i)
    89. res_son.append(cursor.fetchone())
    90. cursor.execute(SQL_Select % id)
    91. res_self=cursor.fetchall()
    92. res.append(res_self)
    93. res.append(res_son)
    94. print(res)
    95. else:
    96. res_self=[]
    97. cursor.execute(SQL_Select % id)
    98. res_self=cursor.fetchall()
    99. print(res_self)
    100. #except:
    101. # print("Select Error!")
    102. db.close()
    103. def UpdateData(name,type_level,father,id): # ok
    104. db = DB()
    105. cursor = db.cursor()
    106. try:
    107. cursor.execute(SQL_Update % (name ,type_level,father,id))
    108. db.commit()
    109. except:
    110. print("Update Error!")
    111. db.close()
    112. def DeleteData(name): # ok
    113. db = DB()
    114. cursor = db.cursor()
    115. cursor.execute(SQL_TypeLevel % name)
    116. type_level = cursor.fetchone()[0]
    117. cursor.execute(SQL_Id % name)
    118. id = cursor.fetchone()[0]
    119. id_son=[]
    120. cursor.execute(SQL_IdSon % name)
    121. for i in range(cursor.rowcount):
    122. id_son.append(cursor.fetchone()[0])
    123. id_grandson=[]
    124. for i in range(len(id_son)):
    125. cursor.execute(SQL_IdGrandSon % id_son[i])
    126. for j in range(cursor.rowcount):
    127. id_grandson.append(cursor.fetchone()[0])
    128. # 判断typelevel,1、2、3。再进行删除
    129. if type_level == 1:
    130. for i in id_grandson:
    131. cursor.execute(SQL_Delete %i)
    132. for j in id_son:
    133. cursor.execute(SQL_Delete %j)
    134. cursor.execute(SQL_Delete % id)
    135. elif type_level == 2:
    136. cursor.execute(SQL_Delete % id_son)
    137. cursor.execute(SQL_Delete % id)
    138. else:
    139. cursor.execute(SQL_Delete % id)
    140. db.commit()
    141. db.close()
    142. if __name__ == "__main__":
    143. main()