def select_row_count(table_name,conn:DB_CON): query="SELECT count(*) FROM "+table_name+" " cur=conn.cursor() cur.execute(query) query_results=cur.fetchall() print(query_results) conn.close()
def exec_query(query:str,conn:DB_CON): cur=conn.cursor() cur.execute(query) query_results=cur.fetchall() print("query run successfull") conn.close() return query_results
def context(conn: Connection) -> Generator[Cursor, None, None]: """ Run multiple PostgreSQL commands in a single connection: with context() as cursor: create_account(cursor, owner) create_database(cursor, owner) """ try: yield conn.cursor() finally: conn.close()
def context(conn: Connection = None, db: str = None) -> Generator[Cursor, None, None]: """ Run multiple MySQL commands in a single connection: >>> with context() as conn, cursor: ... create_account(cursor, owner) ... create_database(cursor, owner) """ conn = conn or connect(db) try: yield conn.cursor() finally: conn.close()
def db_disconnection(con: connection) -> None: try: con.close() print('PostgreSQL connection is closed') except (Exception, psycopg2.DatabaseError) as e: print('Error while disconnecting PostgreSQL:', e)
def close_and_commit_connection(open_connection: connection) -> None: open_connection.commit() open_connection.close() print('[INFO] Database connection closed!')