I’ve seen two approaches which I’m going to post in the comments to see which one is considered best. Feel free to suggest others.

  • GodOfThunder@lemm.eeOP
    link
    fedilink
    arrow-up
    29
    ·
    1 year ago

    A context manager: Create a context manager that handles the connection and cursor creation, as well as closing the connection when done. This way, you can use the with statement to manage the connection and cursor in your functions.

    import sqlite3
    
    DB_FILE = "your_database_file.db"
    
    class DatabaseConnection:
        def __enter__(self):
            self.conn = sqlite3.connect(DB_FILE)
            self.cursor = self.conn.cursor()
            return self.cursor
    
        def __exit__(self, exc_type, exc_val, exc_tb):
            self.conn.commit()
            self.cursor.close()
            self.conn.close()
    
    def insert_post_to_db(issue: Issue, lemmy_post_id: int) -> None:
        with DatabaseConnection() as cursor:
            cursor.execute(
                "INSERT INTO posts (issue_url, lemmy_post_id, issue_title, issue_body) VALUES (?, ?, ?, ?)",
                (issue.url, lemmy_post_id, issue.title, issue.formatted_body),
            )
    
    • coffeewithalex@lemmy.world
      link
      fedilink
      arrow-up
      8
      ·
      1 year ago

      This, but, with DatabaseConnection being a singleton, and preventing multiple enter clauses.

      You can ensure it’s a singleton by modifying how a new object is built, by overriding the new dunder method. If an instance exists, return that, otherwise create a new one.