annotate sqlite_to_tabular.py @ 23:5bb148ee644a draft default tip

Deleted selected files
author jjohnson
date Mon, 17 Jul 2017 15:22:38 -0400
parents bed5018e7ae3
children
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:
22
bed5018e7ae3 Uploaded
jjohnson
parents: 20
diff changeset
34 exit('Error: %s' % (e))
20
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
35 else:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
36 outputFile = sys.stdout
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
37
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
38 query = None
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
39 if (options.query_file is not None):
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
40 with open(options.query_file, 'r') as fh:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
41 query = ''
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
42 for line in fh:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
43 query += line
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
44 elif (options.query is not None):
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
45 query = options.query
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
46
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
47 if (query is None):
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
48 try:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
49 describe_tables(get_connection(options.sqlitedb), outputFile)
22
bed5018e7ae3 Uploaded
jjohnson
parents: 20
diff changeset
50 except Exception as e:
bed5018e7ae3 Uploaded
jjohnson
parents: 20
diff changeset
51 exit('Error: %s' % (e))
20
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
52 exit(0)
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
53 else:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
54 try:
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
55 run_query(get_connection(options.sqlitedb), query, outputFile,
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
56 no_header=options.no_header)
22
bed5018e7ae3 Uploaded
jjohnson
parents: 20
diff changeset
57 except Exception as e:
bed5018e7ae3 Uploaded
jjohnson
parents: 20
diff changeset
58 exit('Error: %s' % (e))
20
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
59
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
60
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
61 if __name__ == "__main__":
ab27c4bd14b9 Uploaded
jjohnson
parents:
diff changeset
62 __main__()