WithSqlite.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Sqls():
  40. objType = ""
  41. def select():
  42. # DISTINCT
  43. sqls = "SELECT FROM {table}".format(table=tableName)
  44. def add():
  45. pass
  46. def delete():
  47. pass
  48. def edit():
  49. pass
  50. def move():
  51. pass
  52. def bp():
  53. pass
  54. def home():
  55. pass
  56. # Regular Start
  57. def Regular(dbPath, exec_commands):
  58. # try:
  59. ie = IsExist(exec_commands)
  60. print("is exist: ", ie)
  61. tableName = str(exec_commands[1]).capitalize()
  62. # to的情况待处理
  63. itemName = str(exec_commands[2])
  64. exec_flag = False
  65. if ie == True:
  66. pass
  67. elif ie == False:
  68. pass
  69. elif exec_flag == True:
  70. # 有个数据库锁定的异常待处理(Multi-user)
  71. Exec_one(dbPath, sqls)
  72. if __name__ == "__main__":
  73. while(1):
  74. sy_i = input("sql: ").split(" ")
  75. Regular(dbPath, sy_i)