def _initialize_connection(self): try: LOG.debug('Trying connection to the database {}', self.db_filename) self.conn = sql.connect(self.db_file_path, check_same_thread=False) cur = self.conn.cursor() cur.execute(str('SELECT SQLITE_VERSION()')) LOG.debug('Database connection {} was successful (SQLite ver. {})', self.db_filename, cur.fetchone()[0]) cur.row_factory = lambda cursor, row: row[0] cur.execute( str('SELECT name FROM sqlite_master WHERE type=\'table\' ' 'AND name NOT LIKE \'sqlite_%\'')) list_tables = cur.fetchall() if not list_tables: # If no tables exist create a new one self.conn.close() db_create_sqlite.create_database(self.db_file_path, self.db_filename) except sql.Error as exc: LOG.error('SQLite error {}:', exc.args[0]) raise DBSQLiteConnectionError from exc finally: if self.conn: self.conn.close()
def _initialize_connection(self): try: common.debug('Trying connection to the database {}'.format(self.db_filename)) self.conn = sql.connect(self.db_file_path) cur = self.conn.cursor() cur.execute(str('SELECT SQLITE_VERSION()')) common.debug('Database connection {} was successful (SQLite ver. {})' .format(self.db_filename, cur.fetchone()[0])) cur.row_factory = lambda cursor, row: row[0] cur.execute(str('SELECT name FROM sqlite_master WHERE type=\'table\' ' 'AND name NOT LIKE \'sqlite_%\'')) list_tables = cur.fetchall() if len(list_tables) == 0: # If no tables exist create a new one self.conn.close() db_create_sqlite.create_database(self.db_file_path, self.db_filename) except sql.Error as e: common.error("SQLite error {}:".format(e.args[0])) raise SQLiteConnectionError finally: if self.conn: self.conn.close()