Created
May 23, 2024 00:40
-
-
Save iqfareez/e08e48cfd7106f6a24b0b38fee502a0d to your computer and use it in GitHub Desktop.
Print all table and its row in an sqlite database
This file contains hidden or 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 | |
import pandas as pd | |
# Connect to the SQLite database (Change to your db name) | |
conn = sqlite3.connect('quran_db.sqlite') | |
# Query to get all table names | |
query = "SELECT name FROM sqlite_master WHERE type='table';" | |
tables = pd.read_sql_query(query, conn) | |
# Initialize a list to hold the table statistics | |
stats = [] | |
# Iterate over all tables and get row counts | |
for table in tables['name']: | |
query = f"SELECT COUNT(*) AS count FROM {table};" | |
count = pd.read_sql_query(query, conn).iloc[0]['count'] | |
stats.append([table, count]) | |
# Convert stats to a DataFrame for easier manipulation | |
stats_df = pd.DataFrame(stats, columns=['Table', 'Rows']) | |
print(stats_df) | |
# Close the connection | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment