annotate sqlite_to_tabular.py @ 2:bc50a7b7f246 draft

Uploaded
author jjohnson
date Sun, 04 Oct 2015 10:51:12 -0400
parents 30a37dd92ccd
children f079ea3884b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
1 #!/usr/bin/env python
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
2
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
3 import sys
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
4 import os.path
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
5 import optparse
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
6 import sqlite3 as sqlite
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
7
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
8 def __main__():
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
9 #Parse Command Line
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
10 parser = optparse.OptionParser()
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
11 parser.add_option( '-s', '--sqlitedb', dest='sqlitedb', default=None, help='The SQLite Database' )
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
12 parser.add_option( '-q', '--query', dest='query', default=None, help='SQL query' )
2
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
13 parser.add_option( '-Q', '--query_file', dest='query_file', default=None, help='SQL query file' )
0
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
14 parser.add_option( '-n', '--no_header', dest='no_header', action='store_true', default=False, help='Include a column headers line' )
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
15 parser.add_option( '-o', '--output', dest='output', default=None, help='Output file for query results' )
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
16 (options, args) = parser.parse_args()
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
17
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
18 # determine output destination
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
19 if options.output != None:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
20 try:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
21 outputPath = os.path.abspath(options.output)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
22 outputFile = open(outputPath, 'w')
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
23 except Exception, e:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
24 print >> sys.stderr, "failed: %s" % e
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
25 exit(3)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
26 else:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
27 outputFile = sys.stdout
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
28
2
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
29 query = None
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
30 if (options.query_file != None):
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
31 with open(options.query_file,'r') as fh:
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
32 query = ''
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
33 for line in fh:
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
34 query += line
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
35 elif (options.query != None):
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
36 query = options.query
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
37
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
38 if (query is None):
0
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
39 try:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
40 conn = sqlite.connect(options.sqlitedb)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
41 c = conn.cursor()
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
42 tables_query = "SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name"
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
43 rslt = c.execute(tables_query).fetchall()
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
44 for table,sql in rslt:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
45 print >> sys.stderr, "Table %s:" % table
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
46 try:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
47 col_query = 'SELECT * FROM %s LIMIT 0' % table
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
48 cur = conn.cursor().execute(col_query)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
49 cols = [col[0] for col in cur.description]
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
50 print >> sys.stderr, " Columns: %s" % cols
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
51 except Exception, exc:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
52 print >> sys.stderr, "Error: %s" % exc
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
53 except Exception, exc:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
54 print >> sys.stderr, "Error: %s" % exc
2
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
55 exit(0)
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
56 #if not sqlite.is_read_only_query(query):
0
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
57 # print >> sys.stderr, "Error: Must be a read only query"
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
58 # exit(2)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
59 try:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
60 conn = sqlite.connect(options.sqlitedb)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
61 cur = conn.cursor()
2
bc50a7b7f246 Uploaded
jjohnson
parents: 0
diff changeset
62 results = cur.execute(query)
0
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
63 if not options.no_header:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
64 outputFile.write("#%s\n" % '\t'.join([str(col[0]) for col in cur.description]))
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
65 # yield [col[0] for col in cur.description]
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
66 for i,row in enumerate(results):
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
67 # yield [val for val in row]
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
68 outputFile.write("%s\n" % '\t'.join([str(val) for val in row]))
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
69 except Exception, exc:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
70 print >> sys.stderr, "Error: %s" % exc
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
71 exit(1)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
72
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
73 if __name__ == "__main__": __main__()
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
74