import sqlite3 def print_sqlite_db_contents(db_path): """ Connects to a SQLite database, prints the name of each table, and then prints all rows and columns for each table. Args: db_path (str): The path to the SQLite database file. """ conn = None try: conn = sqlite3.connect(db_path) cursor = conn.cursor() # 1. Get all table names cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") tables = cursor.fetchall() if not tables: print(f"No tables found in the database: {db_path}") return print(f"--- Contents of SQLite Database: {db_path} ---") for table_name_tuple in tables: table_name = table_name_tuple[0] print(f"\n--- Table: {table_name} ---") # Get column names for the current table cursor.execute(f"PRAGMA table_info({table_name});") column_info = cursor.fetchall() column_names = [col[1] for col in column_info] print(f"Columns: {', '.join(column_names)}") # Select all data from the current table cursor.execute(f"SELECT * FROM {table_name};") rows = cursor.fetchall() if not rows: print(" (Table is empty)") else: for row in rows: print(row) except sqlite3.Error as e: print(f"SQLite error: {e}") finally: if conn: conn.close() print("\n--- Database connection closed ---") # --- Example Usage --- if __name__ == "__main__": # Create a dummy database for demonstration dummy_db_path = "./tmp/padel.db" # Now, call the function to print its contents print_sqlite_db_contents(dummy_db_path) # You can also use it with an existing database, e.g.: # print_sqlite_db_contents("your_actual_database.db") # Clean up the dummy database #import os #if os.path.exists(dummy_db_path): #os.remove(dummy_db_path) #print(f"\nDummy database '{dummy_db_path}' removed.")