Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create safe query function #3

Open
obitech opened this issue Nov 2, 2019 · 0 comments
Open

Create safe query function #3

obitech opened this issue Nov 2, 2019 · 0 comments

Comments

@obitech
Copy link
Member

obitech commented Nov 2, 2019

We need to make sure the connection and cursor is properly closed even if an exception is thrown.

Idea:

# ... other imports ...
from contextlib import closing

def query(db_name, sql):
    """Performs a database query and returns the result.

    It will uses contextlib to safely wrap a sqlite3 connection and
    a cursor to ensure the connection safely commits and closes (same
    as the cursor) in both happy and sad paths.

    Args:
        db_name (str): The name of the sqlite3 database to open.
        sql (str): The SQL query to perform.
    
    Returns:
        list: A list of tuples that gets returned by the performed query,
            as with calling ``cursor.fetchall()``. This will return an empty
            list in case nothing gets returned (such as for an UPDATE).
    """
    # Auto-closes
    with closing(sqlite3.connect(db_name)) as conn:
        # Auto-commits
        with conn:
            # Auto-closes
            with contextlib.closing(conn.cursor()) as cur:
                cur.execute(sql)
                return cur.fetchall()

You can then use wrap this in the try...except block:

try:
    # This will create a tuple that can be passed to the cursor
    query_tuple = "insert into Events values (?,?,?,?,?,?)", # .... 
    query(DB_NAME, query_tuple)
except sqlite3.IntegrityError as e:
    logging.error(e)

This needs to be unit-tested as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant