Created
January 31, 2025 08:42
-
-
Save dado3212/d6d69b30d64c56a7f684b3ee81cb5f84 to your computer and use it in GitHub Desktop.
Search all tables in a SQLite database for a specific column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
def search_in_all_tables(db_path, text): | |
conn = sqlite3.connect(db_path) | |
cursor = conn.cursor() | |
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'") | |
tables = [t[0] for t in cursor.fetchall()] | |
results = [] | |
for table in tables: | |
cursor.execute(f"PRAGMA table_info({table})") | |
columns = [c[1] for c in cursor.fetchall()] | |
for col in columns: | |
query = f"SELECT rowid, * FROM {table} WHERE {col} LIKE ?" | |
cursor.execute(query, (f"%{text}%",)) | |
found = cursor.fetchall() | |
for row in found: | |
results.append((table, col, row)) | |
conn.close() | |
return results | |
results = search_in_all_tables('/Users/abeals/Library/Group Containers/243LU875E5.groups.com.apple.podcasts/Documents/MTLibrary.sqlite', '1000470600560') | |
for result in results: | |
print(result) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment