annotate sqlite_to_tabular.py @ 21:357fe86f245d draft

Uploaded
author jjohnson
date Fri, 14 Jul 2017 17:34:22 -0400
parents ab27c4bd14b9
children bed5018e7ae3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
1 #!/usr/bin/env python
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
2
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
3 from __future__ import print_function
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
4
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
5 import optparse
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
6 import os.path
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
7 import sys
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
8
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
9 from query_db import describe_tables, get_connection, run_query
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
10
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
11
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
12 def __main__():
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
13 # Parse Command Line
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
14 parser = optparse.OptionParser()
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
15 parser.add_option('-s', '--sqlitedb', dest='sqlitedb', default=None,
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
16 help='The SQLite Database')
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
17 parser.add_option('-q', '--query', dest='query', default=None,
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
18 help='SQL query')
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
19 parser.add_option('-Q', '--query_file', dest='query_file', default=None,
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
20 help='SQL query file')
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
21 parser.add_option('-n', '--no_header', dest='no_header', default=False,
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
22 action='store_true',
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
23 help='Include a column headers line')
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
24 parser.add_option('-o', '--output', dest='output', default=None,
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
25 help='Output file for query results')
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
26 (options, args) = parser.parse_args()
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
27
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
28 # determine output destination
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
29 if options.output is not None:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
30 try:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
31 outputPath = os.path.abspath(options.output)
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
32 outputFile = open(outputPath, 'w')
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
33 except Exception as e:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
34 print("failed: %s" % e, file=sys.stderr)
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
35 exit(3)
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
36 else:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
37 outputFile = sys.stdout
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
38
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
39 query = None
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
40 if (options.query_file is not None):
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
41 with open(options.query_file, 'r') as fh:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
42 query = ''
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
43 for line in fh:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
44 query += line
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
45 elif (options.query is not None):
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
46 query = options.query
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
47
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
48 if (query is None):
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
49 try:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
50 describe_tables(get_connection(options.sqlitedb), outputFile)
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
51 except Exception as exc:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
52 print("Error: %s" % exc, file=sys.stderr)
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
53 exit(0)
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
54 else:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
55 try:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
56 run_query(get_connection(options.sqlitedb), query, outputFile,
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
57 no_header=options.no_header)
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
58 except Exception as exc:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
59 print("Error: %s" % exc, file=sys.stderr)
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
60 exit(1)
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
61
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
62
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
63 if __name__ == "__main__":
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
64 __main__()