sqlite.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import sqlite3
  2. import Stateful
  3. deType = "sqlite3"
  4. dbPath = "dev.db"
  5. # [todo 1]
  6. # 1. 检查commands[5]是否为dbPath, 不是话报错(syntax error)
  7. # ↑↑↑ 但是总感觉输入检查应该放在Controller里面(也就是放在前端去检查)...
  8. # 前端检查语法, 冲突代回到后端去检查, 然后顺便就执行了
  9. # 2. 命令里面的<KB>, <CL>, <EV>都代入检查是否存在, 区分add/edit(INSERT INTO/UPDATE)
  10. # 3. 匹配SQL关键字
  11. # 4. 拼完以后进行to和in的判断
  12. # 5. 按正确的顺序拼SQL语句
  13. # 6. 执行
  14. class Op_sqlite3():
  15. def __init__(self, commands) -> None:
  16. self.dbPath = dbPath
  17. self.commands = commands
  18. self.tableName = str(commands[1]).capitalize()
  19. self.columnName = str(commands[2])
  20. # "to" condition need a "if"
  21. self.ItemName = str(commands[])
  22. def Exec(self):
  23. con = sqlite3.connect(dbPath)
  24. cur = con.cursor()
  25. cur.execute(self.commands)
  26. con.commit()
  27. re = cur.fetchall()
  28. con.close()
  29. return re
  30. def IsExist(self, returnBool=True):
  31. sqls = "SELECT {coumn} FROM {table} WHERE name='{name}'".format(column=self.columnName, table=self.tableName, name=self.ItemName)
  32. ie = Op_sqlite3.Exec(sqls)
  33. if ie != None and returnBool == False:
  34. return ie
  35. elif ie != None and returnBool == True:
  36. return True
  37. elif ie == None:
  38. return False
  39. else:
  40. print("unexpected processing error")
  41. def Secondary_response(self):
  42. pass
  43. # Regular Start
  44. def Regular(self):
  45. try:
  46. ie = Op_sqlite3.IsExist()
  47. if ie != None:
  48. Op_sqlite3.Exec(dbPath, sy_i)
  49. except:
  50. print("not exist")
  51. if __name__ == "__main__":
  52. while(1):
  53. sy_i = input("sql: ").split(" ")
  54. Op_sqlite3.Regular(dbPath, sy_i)