sqlite.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import sqlite3
  2. import Stateful
  3. deType = "sqlite3"
  4. dbPath = "d<EV>.db"
  5. def is_exist(tableName, columnName):
  6. pass
  7. def Secondary_response():
  8. pass
  9. def Operate_sqlite3(dbPath, commands):
  10. # Command e.g.
  11. # 0 1 2 3 4 5 6
  12. # [add]
  13. # ['add', 'board', '<KB>']
  14. # ['add', 'class', '<CL>', 'to', '<KB>']
  15. # ['add', 'event', '<EV>', 'to', '<KB>', '/', '<CL>'] <- 这个应该报错
  16. # ['add', 'event', '<EV>', 'to', '<KB>/<CL>']
  17. # [edit]
  18. # ['edit', 'board', '<KB>', 'to', '<KB>']
  19. # ['edit', 'class', '<KB>', 'to', '<KB>']
  20. # ['edit', 'class', '<CL>', 'to', '<CL>']
  21. # ['edit', 'class', '<CL>', 'to', '<CL>']
  22. # ['edit', 'event', '<EV>', 'to', '<EV>']
  23. # ['edit', 'event', '<EV>', 'in', '<KB>', '/', '<CL>'] <- 这个应该报错
  24. # ['edit', 'event', '<EV>', 'in', '<KB>/<CL>']
  25. # [delete]
  26. # ['delete', 'board', '<KB>']
  27. # ['delete', 'class', '<CL>']
  28. # ['delete', 'event', '<EV>']
  29. # ['delete', 'event', '<EV>', 'in', '<KB>', '/', '<CL>'] <- 这个应该报错
  30. # ['delete', 'event', '<EV>', 'in', '<KB>/<CL>']
  31. # [move]
  32. # ['move', 'class', '<CL>', 'to', '<KB>']
  33. # ['move', 'event', '<EV>', 'to', '<KB>']
  34. # ['move', 'event', '<EV>', 'to', '<CL>']
  35. # ['move', 'event', '<EV>', 'in', '<KB>', '/', '<CL>'] <- 这个应该报错
  36. # ['move', 'event', '<EV>', 'in', '<KB>/<CL>']
  37. # [select]
  38. # ['select', 'board', '<KB>']
  39. # ['select', 'class', '<CL>']
  40. # ['select', 'event', '<EV>']
  41. # ['select', 'event', '<EV>', 'in', '<KB>', '/', '<CL>'] <- 这个应该报错
  42. # ['select', 'event', '<EV>', 'in', '<KB>/<CL>']
  43. # [..]
  44. # [/]
  45. # 1. 检查commands[5]是否非有'/', 有的话报错(syntax error)
  46. # 2. 命令里面的<KB>, <CL>, <EV>都代入检查是否存在, 区分add/edit(INSERT INTO/UPDATE)
  47. # 3. 拼SQL语句
  48. v = "SELECT * FROM {tableName} WHERE name={name}".format(tableName=str(commands[1]).capitalize(), name="'"+commands[2]+"'")
  49. con = sqlite3.connect(dbPath)
  50. cur = con.cursor()
  51. # 1. exist check
  52. try:
  53. res = cur.execute(v)
  54. con.commit()
  55. print("res: ", res.fetchone() is None)
  56. # syntax right but not exist:
  57. if res.fetchone() is None == True:
  58. print("err <Code>: could not found, creat? y/n")
  59. # Secondary_response()
  60. # 2. exec command
  61. matched = Stateful.MatchTomlKeys('d<EV>_config.toml', commands, 'sqlite3')
  62. print(matched)
  63. except:
  64. print("err <Code>: Syntax error")
  65. con.close()
  66. if __name__ == "__main__":
  67. while(1):
  68. sy_i = input("sql: ").split(" ")
  69. Operate_sqlite3(dbPath, sy_i)