WithSqlite.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import sqlite3
  2. # [todo 1]
  3. # 1. 检查commands[5]是否为dbPath, 不是话报错(syntax error)
  4. # ↑↑↑ 但是总感觉输入检查应该放在Controller里面(也就是放在前端去检查)...
  5. # 前端检查语法, 冲突代回到后端去检查, 然后顺便就执行了
  6. # 2. 命令里面的<KB>, <CL>, <EV>都代入检查是否存在, 区分add/edit(INSERT INTO/UPDATE)
  7. # 3. 匹配SQL关键字
  8. # 4. 拼完以后进行to和in的判断
  9. # 5. 按正确的顺序拼SQL语句
  10. # 6. 执行
  11. dbPath = "dev.db"
  12. def Exec_one(dbPath, commands):
  13. con = sqlite3.connect(dbPath)
  14. cur = con.cursor()
  15. cur.execute(commands)
  16. con.commit()
  17. re = cur.fetchall()
  18. con.close()
  19. return re
  20. def Exec_many():
  21. pass
  22. def IsExist(exec_commands, returnBool=True):
  23. tableName = str(exec_commands[1]).capitalize()
  24. ItemName = str(exec_commands[2])
  25. sqls = "SELECT name FROM {table} WHERE name='{name}'".format(table=tableName, name=ItemName)
  26. ie = Exec_one(dbPath, sqls)
  27. if ie != [] and returnBool == False:
  28. return ie
  29. elif ie != [] and returnBool == True:
  30. return True
  31. elif ie == []:
  32. return False
  33. else:
  34. # Alt.Err(errCode)
  35. print("err <Code>: unexpected error in existence check")
  36. # [todo 4]
  37. def Secondary_response():
  38. pass
  39. class KB():
  40. pass
  41. class CL():
  42. pass
  43. class EV():
  44. pass
  45. # Regular Start
  46. def Regular(dbPath, exec_commands):
  47. # try:
  48. ie = IsExist(exec_commands)
  49. print("is exist: ", ie)
  50. tableName = str(exec_commands[1]).capitalize()
  51. # to的情况待处理
  52. itemName = str(exec_commands[2])
  53. exec_flag = False
  54. if ie == True:
  55. if exec_commands[0] == 'select':
  56. sqls = "SELECT name FROM {table} WHERE name='{name}';".format(table=tableName, name=itemName)
  57. exec_flag = True
  58. elif exec_commands[0] == 'add':
  59. print("err <Code>: existed")
  60. elif exec_commands[0] == 'delete':
  61. sqls = "UPDATE {table} SET status='deleted' WHERE name='{name}';".format(table=tableName, name=itemName)
  62. exec_flag = True
  63. elif exec_commands[0] == 'edit':
  64. sqls = ""
  65. exec_flag = True
  66. elif exec_commands[0] == 'move':
  67. sqls = ""
  68. exec_flag = True
  69. elif exec_commands[0] == '..':
  70. sqls = ""
  71. exec_flag = True
  72. elif exec_commands[0] == '/':
  73. sqls = ""
  74. exec_flag = True
  75. elif exec_commands[0] == 'wipe':
  76. sqls = ""
  77. exec_flag = True
  78. elif ie == False:
  79. if exec_commands[0] == 'select':
  80. print("err <Code>: not exist")
  81. elif exec_commands[0] == 'add':
  82. sqls = "INSERT INTO {table} VALUES(null, '{name}', 'live')".format(table=tableName, name=itemName)
  83. exec_flag = True
  84. elif exec_commands[0] == 'delete':
  85. sqls = ""
  86. exec_flag = True
  87. elif exec_commands[0] == 'edit':
  88. sqls = ""
  89. exec_flag = True
  90. elif exec_commands[0] == 'move':
  91. print("err <Code>: not exist")
  92. elif exec_commands[0] == '..':
  93. sqls = ""
  94. exec_flag = True
  95. elif exec_commands[0] == '/':
  96. print("err <Code>: not exist")
  97. elif exec_commands[0] == 'wipe':
  98. sqls = ""
  99. exec_flag = True
  100. elif exec_flag == True:
  101. # 有个数据库锁定的异常待处理(Multi-user)
  102. Exec_one(dbPath, sqls)
  103. if __name__ == "__main__":
  104. while(1):
  105. sy_i = input("sql: ").split(" ")
  106. Regular(dbPath, sy_i)