sqlite.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import sqlite3
  2. import Stateful
  3. deType = "sqlite3"
  4. dbPath = "dev.db"
  5. def is_exist(tableName, columnName):
  6. pass
  7. # [todo 3]
  8. def Secondary_response():
  9. pass
  10. def Operate_sqlite3(dbPath, commands):
  11. # Commands e.g.
  12. # 0 1 2 3 4 5 6
  13. # [add]
  14. # ['add', 'board', '<KB>']
  15. # ['add', 'class', '<CL>', 'to', '<KB>']
  16. # ['add', 'event', '<EV>', 'to', '<KB>', '/', '<CL>'] <- 这个应该报错
  17. # ['add', 'event', '<EV>', 'to', '<KB>/<CL>']
  18. # [edit]
  19. # ['edit', 'board', '<KB>', 'to', '<KB>']
  20. # ['edit', 'class', '<KB>', 'to', '<KB>']
  21. # ['edit', 'class', '<CL>', 'to', '<CL>']
  22. # ['edit', 'class', '<CL>', 'to', '<CL>']
  23. # ['edit', 'event', '<EV>', 'to', '<EV>']
  24. # ['edit', 'event', '<EV>', 'in', '<KB>', '/', '<CL>'] <- 这个应该报错
  25. # ['edit', 'event', '<EV>', 'in', '<KB>/<CL>']
  26. # [delete]
  27. # ['delete', 'board', '<KB>']
  28. # ['delete', 'class', '<CL>']
  29. # ['delete', 'event', '<EV>']
  30. # ['delete', 'event', '<EV>', 'in', '<KB>', '/', '<CL>'] <- 这个应该报错
  31. # ['delete', 'event', '<EV>', 'in', '<KB>/<CL>']
  32. # [move]
  33. # ['move', 'class', '<CL>', 'to', '<KB>']
  34. # ['move', 'event', '<EV>', 'to', '<KB>']
  35. # ['move', 'event', '<EV>', 'to', '<CL>']
  36. # ['move', 'event', '<EV>', 'in', '<KB>', '/', '<CL>'] <- 这个应该报错
  37. # ['move', 'event', '<EV>', 'in', '<KB>/<CL>']
  38. # [select]
  39. # ['select', 'board', '<KB>']
  40. # ['select', 'class', '<CL>']
  41. # ['select', 'event', '<EV>']
  42. # ['select', 'event', '<EV>', 'in', '<KB>', '/', '<CL>'] <- 这个应该报错
  43. # ['select', 'event', '<EV>', 'in', '<KB>/<CL>']
  44. # [..]
  45. # [/]
  46. # [todo 1]
  47. # 1. 检查commands[5]是否为dbPath, 不是话报错(syntax error)
  48. # ↑↑↑ 但是总感觉输入检查应该放在Controller里面(也就是放在前端去检查)...
  49. # 前端检查语法, 冲突代回到后端去检查, 然后顺便就执行了
  50. # 2. 命令里面的<KB>, <CL>, <EV>都代入检查是否存在, 区分add/edit(INSERT INTO/UPDATE)
  51. # 3. 匹配SQL关键字
  52. # 4. 拼完以后进行to和in的判断
  53. # 5. 按正确的顺序拼SQL语句
  54. # 6. 执行
  55. v = "SELECT * FROM {tableName} WHERE name={name}".format(tableName=str(commands[1]).capitalize(), name="'"+commands[2]+"'")
  56. con = sqlite3.connect(dbPath)
  57. cur = con.cursor()
  58. # 1. exist check
  59. try:
  60. res = cur.execute(v)
  61. con.commit()
  62. print("res: ", res.fetchone() is None)
  63. # syntax right but not exist:
  64. if res.fetchone() is None == True:
  65. print("err <Code>: could not found, creat? y/n")
  66. # Secondary_response()
  67. # 2. exec command
  68. matched = Stateful.MatchTomlKeys('dev_config.toml', commands, 'sqlite3')
  69. print(matched)
  70. except:
  71. print("err <Code>: Syntax error")
  72. con.close()
  73. if __name__ == "__main__":
  74. while(1):
  75. sy_i = input("sql: ").split(" ")
  76. Operate_sqlite3(dbPath, sy_i)