import contextlib
import sqlite3


def sqlite3_safe_execute_binary(db_path:str, statement:str, binary_vals:tuple, fetch:bool=False):
    """
    execute statement with auto-commit and connection/cursor auto-close
    
    Return:
    cursor: sqlite3.conn.cursor
    """
    with contextlib.closing(sqlite3.connect(db_path)) as conn: # auto-closes
        with conn: # auto-commits
            with contextlib.closing(conn.cursor()) as cursor: # auto-closes
                cur = cursor.execute(statement, binary_vals)
                if fetch==True:
                    return cur.fetchall()