我正在尝试使用下面附加的代码执行SQLite命令来创建表。
这两个命令可以正常工作:
1. CREATE TABLE IF NOT EXISTS platform_thresholds (
id integer PRIMARY KEY,
platform TEXT ,
protocol TEXT ,
threshold TEXT ,
value TEXT );
2. CREATE TABLE IF NOT EXISTS dev_protocol (
id integer PRIMARY KEY,
dev_ip TEXT ,
dev_platform TEXT ,
protocol TEXT ,
param TEXT ,
value TEXT );
但以下内容会给出一个错误:
3. CREATE TABLE IF NOT EXISTS diag_input (
id integer PRIMARY KEY,
dev_ip TEXT ,
dev_platform TEXT ,
check TEXT ,
protocol TEXT ,
diagnostics TEXT );
sqlite3.OperationalError:Near“Text”:语法错误
我不太明白这里的问题--有人能提供一些见解吗?
以下是我用于连接SQLite的代码:
class SQLiteIF:
def __init__(*args, **kwargs):
self = args[0]
self.__db_file = kwargs.get('dbfile')
if not self.__db_file:
db_path = os.path.join(BASE_PATH, '__dbs')
if not os.path.exists(db_path):
os.makedirs(db_path)
self.__db_file = os.path.join(db_path, f'db_{time_stamp(file_stamp=True, filler="")}')
self.__conn = None
self.__cur = None
conn = None
try:
conn = sqlite3.connect(self.__db_file, isolation_level=None)
except Error as e:
debug(msg=f'Exception {format_exc()} occurred while creating database {self.__db_file}',
alt_text='Error creating database')
else:
debug(msg=f'Successful in creating database {self.__db_file}', alt_text='database created successfully')
finally:
if conn:
conn.close()
def __connect(self):
try:
self.__conn = sqlite3.connect(self.__db_file, isolation_level=None)
except Error as e:
if self.__conn:
self.__conn.close()
self.__conn = None
debug(msg=f'Exception {format_exc()} occurred while creating database {self.__db_file}')
return ReturnCodes.EXCEPTION, debug(alt_text='Unable to create database')
else:
return ReturnCodes.OK, debug(msg=f'Successful in connecting to database {self.__db_file}')
def exec_cmd(self, cmd, step, close_con=True):
res = self.__connect()
if res[0] != ReturnCodes.OK:
return res
try:
self.__cur = self.__conn.cursor()
self.__cur.execute(cmd) # <- POINT OF EXCEPTION
self.__conn.commit()
except Error as e:
debug(msg=f'Exception {format_exc()} occurred while performing "{step}" for DB "{self.__db_file}"')
return ReturnCodes.EXCEPTION, debug(alt_text=f'Unable to perform SQLite operation: "{step}"')
finally:
if close_con:
self.__conn.close()
return ReturnCodes.OK, debug(msg=f'Successfully able to perform SQLite operation: "{step}"')
调用命令执行器以创建表
SQLiteIF.exec_cmd('CREATE TABLE IF NOT EXISTS platform_thresholds (
id integer PRIMARY KEY,
platform TEXT ,
protocol TEXT ,
threshold TEXT ,
value TEXT );')
SQLiteIF.exec_cmd('CREATE TABLE IF NOT EXISTS dev_protocol (
id integer PRIMARY KEY,
dev_ip TEXT ,
dev_platform TEXT ,
protocol TEXT ,
param TEXT ,
value TEXT );')
# THIS THROWS EXCEPTION - sqlite3.OperationalError: near "TEXT": syntax error
SQLiteIF.exec_cmd('CREATE TABLE IF NOT EXISTS diag_input (
id integer PRIMARY KEY,
dev_ip TEXT ,
dev_platform TEXT ,
check TEXT ,
protocol TEXT ,
diagnostics TEXT );')
1条答案
按热度按时间ej83mcc01#
OK-收到问题在调试模式下处理参数时似乎很快:_(
命令的“Check Text”部分
因此错误地尝试使用SQL中的保留约束关键字‘check’标题创建列名。