Created
February 11, 2015 04:05
-
-
Save n8henrie/2a8cf647f0aea9f7e7f3 to your computer and use it in GitHub Desktop.
Accepts an sql database and converts all tables to separate csv files.
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
#! /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