Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Created March 15, 2013 23:14
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 joyrexus/5173924 to your computer and use it in GitHub Desktop.
Save joyrexus/5173924 to your computer and use it in GitHub Desktop.
Convert a tab-separated-value formatted file to a set of SQL insert or update statements
#!/usr/bin/env python
'''
Convert a TSV-formatted file to a set of SQL insert or update statements.
'''
import argparse
from datastore.table import Reader
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--mode', choices=('insert', 'update'), default='update')
parser.add_argument('--table', default='items')
parser.add_argument('files', nargs='+')
args = parser.parse_args()
for file in args.files:
r = Reader(file)
if args.mode == 'insert':
print r.batch_insert_sql(table=args.table)
elif args.mode == 'update':
print r.batch_update_sql(table=args.table)
else:
parser.print_help()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment