0

I'm coding in Python and using JayDeBeApi to connect to a MS Access database. The problem is I can't seem to find how to close the file, cause when I run a script to compact the database (msaccess %path_to_db% /compact) it says file already in use.

My code look like this

class AccessConnect:
    def __init__(self, p_path_to_db: Path, p_table_name: str)
        self.path_to_db = p_path_to_db
        self.table_name = p_table_name

        self.u_can_access_jars = [
            f"{ref_to_root}/jdbc_access/ucanaccess-5.0.1.jar",
            f"{ref_to_root}/jdbc_access/commons-lang3-3.8.1.jar",
            f"{ref_to_root}/jdbc_access/commons-logging-1.2.jar",
            f"{ref_to_root}/jdbc_access/hsqldb-2.5.0.jar",
            f"{ref_to_root}/jdbc_access/jackcess-3.0.1.jar"
        ]
        self.classpath = ";".join(self.u_can_access_jars)

        try:
            jpype.startJVM(classpath=self.u_can_access_jars)
        except OSError as e:
            if e.args[0] == "JVM is already started":
                pass
            else:
                raise e

    def open_connexion(self) -> jaydebeapi.Connection:
        return jaydebeapi.connect(
            "net.ucanaccess.jdbc.UcanaccessDriver",
            f"jdbc:ucanaccess:///{self.path_to_db}",
            ["", ""],
            self.classpath
        )

    def get_all(self):
        query = f"SELECT * FROM {self.table_name}"
        with self.open_connection() as conn:
            with conn.cursor() as cursor:
                cursor.execute(query)
                ans = cursor.fetchall()
       return ans

So I want to be able to close the file to use it after.

Thanks!

0 Answers0