import sqlite3 import Stateful deType = "sqlite3" dbPath = "dev.db" def is_exist(tableName, columnName): pass def Secondary_response(): pass def Operate_sqlite3(dbPath, commands): # Command e.g. # 0 1 2 3 4 5 6 # [add] # ['add', 'board', '<KB>'] # ['add', 'class', '<CL>', 'to', '<KB>'] # ['add', 'event', '<EV>', 'to', '<KB>', '/', '<CL>'] <- 这个应该报错 # ['add', 'event', '<EV>', 'to', '<KB>/<CL>'] # [edit] # ['edit', 'board', '<KB>', 'to', '<KB>'] # ['edit', 'class', '<KB>', 'to', '<KB>'] # ['edit', 'class', '<CL>', 'to', '<CL>'] # ['edit', 'class', '<CL>', 'to', '<CL>'] # ['edit', 'event', '<EV>', 'to', '<EV>'] # ['edit', 'event', '<EV>', 'in', '<KB>', '/', '<CL>'] <- 这个应该报错 # ['edit', 'event', '<EV>', 'in', '<KB>/<CL>'] # [delete] # ['delete', 'board', '<KB>'] # ['delete', 'class', '<CL>'] # ['delete', 'event', '<EV>'] # ['delete', 'event', '<EV>', 'in', '<KB>', '/', '<CL>'] <- 这个应该报错 # ['delete', 'event', '<EV>', 'in', '<KB>/<CL>'] # [move] # ['move', 'class', '<CL>', 'to', '<KB>'] # ['move', 'event', '<EV>', 'to', '<KB>'] # ['move', 'event', '<EV>', 'to', '<CL>'] # ['move', 'event', '<EV>', 'in', '<KB>', '/', '<CL>'] <- 这个应该报错 # ['move', 'event', '<EV>', 'in', '<KB>/<CL>'] # [select] # ['select', 'board', '<KB>'] # ['select', 'class', '<CL>'] # ['select', 'event', '<EV>'] # ['select', 'event', '<EV>', 'in', '<KB>', '/', '<CL>'] <- 这个应该报错 # ['select', 'event', '<EV>', 'in', '<KB>/<CL>'] # [..] # [/] # 1. 检查commands[5]是否非有'/', 有的话报错(syntax error) # 2. 命令里面的<KB>, <CL>, <EV>都代入检查是否存在, 区分add/edit(INSERT INTO/UPDATE) # 3. 拼SQL语句 v = "SELECT * FROM {tableName} WHERE name={name}".format(tableName=str(commands[1]).capitalize(), name="'"+commands[2]+"'") con = sqlite3.connect(dbPath) cur = con.cursor() # 1. exist check try: res = cur.execute(v) con.commit() print("res: ", res.fetchone() is None) # syntax right but not exist: if res.fetchone() is None == True: print("err <Code>: could not found, creat? y/n") # Secondary_response() # 2. exec command matched = Stateful.MatchTomlKeys('d<EV>_config.toml', commands, 'sqlite3') print(matched) except: print("err <Code>: Syntax error") con.close() if __name__ == "__main__": while(1): sy_i = input("sql: ").split(" ") Operate_sqlite3(dbPath, sy_i)