Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Created February 11, 2015 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8henrie/2a8cf647f0aea9f7e7f3 to your computer and use it in GitHub Desktop.
Save n8henrie/2a8cf647f0aea9f7e7f3 to your computer and use it in GitHub Desktop.
Accepts an sql database and converts all tables to separate csv files.
#! /usr/bin/env python3
"""sql_to_csv.py
Accepts an sql database and converts all tables to separate csv files.
"""
import sqlite3
import csv
import sys
conn = sqlite3.connect(sys.argv[1])
with conn:
cmd = '''select name from sqlite_master where type="table";'''
table_names = conn.execute(cmd).fetchall()
for table_name in table_names:
cmd = '''select * from {};'''.format(table_name[0])
csv_out = conn.execute(cmd).fetchall()
with open(table_name[0] + '.csv', 'w') as w:
writer = csv.writer(w)
writer.writerows(csv_out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment