def insert_new_equation(self, original: str, solution: str, time: float): """ Receives a new equation's details and inserts it to the table. May Raise a SQLiteError if the contact's number already exists. May raise a SQLiteError if there was an error with the SQL query. May raise a SQLiteError if there was an error with the database when inserting the data. """ insert_query = DBConsts.INSERT_EQUATION_TEMPLATE.format( DBConsts.EQUATIONS_TABLE_NAME, original, solution, time) try: self.cursor.execute(insert_query) self.connection.commit() except ProgrammingError: raise SQLiteError( f'A programming error occurred when inserting the new equation: {insert_query}' ) except IntegrityError as exception: # If this error occurred, we know it's because there's already a contact with that number. raise SQLiteError( f'An integrity error occurred when inserting the new equation: {exception}' ) except DatabaseError as exception: raise SQLiteError( f'A database error occurred when inserting the new equation: {exception}' )
def __init__(self, db_file: str = None): """ May raise an sqlite3.Error error if a problem occurs when connecting to the DB. """ self.db_file = DBConsts.DB_SAVE_FILE if db_file is None else None try: self.connection = sqlite3.connect(self.db_file) self.cursor = self.connection.cursor() except DatabaseError as exception: raise SQLiteError( f'A database error occurred when connecting to the DB: {exception}' ) except OperationalError as exception: raise SQLiteError( f'An operational error occurred when connecting to the DB: {exception}' )
def execute( # type: ignore[override] self, sql: str, parameters: Optional[Mapping] = None) -> BaseCursor: if parameters is None: parameters = {} if self._generateErrors: raise SQLiteError("execute()") return super().execute(sql, parameters)
def close(self): """ May Raise a Database Error if an error occurred when closing the database or the cursor. """ try: self.cursor.close() self.connection.close() except OperationalError as exception: raise SQLiteError( f'An operation error occurred when closing the connection\\cursor: {exception}' )
def initialize_equations_table(self): """ This function creates an empty equation table in the equations database. """ table_initialize_query = DBConsts.CREATE_TABLE_TEMPLATE.format( DBConsts.EQUATIONS_TABLE_NAME) try: self.cursor.execute(table_initialize_query) except ProgrammingError: raise SQLiteError( f'A programming error occurred when creating the contacts table: {table_initialize_query}' ) except DatabaseError as exception: raise SQLiteError( f'A database error occurred when creating the contacts table: {exception}' ) except OperationalError as exception: raise SQLiteError( f'An operational error occurred when creating the contacts table: {exception}' )
def openDB(path: Path, schema: Optional[str] = None) -> Connection: """ Open an SQLite DB with the schema for this application. """ if path.exists(): return connect(path) if schema is not None: return createDB(path, schema) raise SQLiteError(f"Database does not exist: {path}")
def execute(self, sql: str, parameters: Mapping = None) -> BaseCursor: if self._generateErrors: raise SQLiteError("execute()") return super().execute(sql, parameters)
def executescript(self, sql_script: str) -> BaseCursor: if self._generateErrors: raise SQLiteError("executescript()") return super().executescript(sql_script)