Skip to content

Instantly share code, notes, and snippets.

@PC-CNT
Created June 21, 2024 10:10
Show Gist options
  • Select an option

  • Save PC-CNT/94b2d577bf5c4f5d60cb81a38ba7aefb to your computer and use it in GitHub Desktop.

Select an option

Save PC-CNT/94b2d577bf5c4f5d60cb81a38ba7aefb to your computer and use it in GitHub Desktop.
import sqlite3
import sys
import pandas as pd
def export_all_tables_to_csv(db_file):
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
for table_name in tables:
table_name = table_name[0]
df = pd.read_sql_query(f"SELECT * FROM {table_name}", conn)
df.to_csv(f"{table_name}.csv", index=False)
print(f"Table {table_name} exported to {table_name}.csv")
conn.close()
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python sqlite_csv.py <database_file>")
sys.exit(1)
db_file = sys.argv[1]
export_all_tables_to_csv(db_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment