'sqlite3.OperationalError: near "TEXT": syntax error' in one table creation command but other tables are created without issue

Viewed 36

I am trying to execute the SQLite command to create tables using the code attached below.

These two commands work without issue:

 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 );

but the following gives an error:

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": syntax error

I don't quite understand the issue over here - could someone please provide some insight into this.

Following is my code for interfacing 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}"')

Calling command executor for table creation

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 Answers

Ok - got the issue seemingly quickly while playing with params in debug mode :_(

"check TEXT" part of the command

CREATE TABLE IF NOT EXISTS diag_input (
    id integer PRIMARY KEY,
    dev_ip TEXT,
    dev_platform TEXT,
    **check TEXT**,
    protocol TEXT,
    diagnostics TEXT
);

So mistakenly trying to create column name with 'Check' title which is reserved constraint keyword in sql.

Related