sqlite.py 3.1 KB

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