Skip to content

Instantly share code, notes, and snippets.

@recrsn
Created May 29, 2023 17:43
Show Gist options
  • Select an option

  • Save recrsn/69e82ca01eee5f1b5c84f479691da234 to your computer and use it in GitHub Desktop.

Select an option

Save recrsn/69e82ca01eee5f1b5c84f479691da234 to your computer and use it in GitHub Desktop.
import sqlite3
# Path to your SQLite file
sqlite_file = 'your_database.sqlite'
# Connect to the SQLite database
conn = sqlite3.connect(sqlite_file)
cursor = conn.cursor()
# Get the table names
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
# Loop through the tables
for table in tables:
table_name = table[0]
print(f"Table: {table_name}")
print("Columns:")
# Get the column names for the current table
cursor.execute(f"PRAGMA table_info({table_name});")
columns = cursor.fetchall()
# Loop through the columns
for column in columns:
column_name = column[1]
print(f"- {column_name}")
print() # Add an empty line between tables
# Close the database connection
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment