annotate sqlite_to_tabular.py @ 0:30a37dd92ccd draft

Uploaded
author jjohnson
date Sat, 03 Oct 2015 09:13:55 -0400
parents
children bc50a7b7f246
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' )
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
13 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
14 parser.add_option( '-o', '--output', dest='output', default=None, help='Output file for query results' )
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
15 (options, args) = parser.parse_args()
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
16
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
17 # determine output destination
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
18 if options.output != None:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
19 try:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
20 outputPath = os.path.abspath(options.output)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
21 outputFile = open(outputPath, 'w')
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
22 except Exception, e:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
23 print >> sys.stderr, "failed: %s" % e
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
24 exit(3)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
25 else:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
26 outputFile = sys.stdout
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
27
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
28 if (options.query is None):
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
29 try:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
30 conn = sqlite.connect(options.sqlitedb)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
31 c = conn.cursor()
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
32 tables_query = "SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name"
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
33 rslt = c.execute(tables_query).fetchall()
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
34 for table,sql in rslt:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
35 print >> sys.stderr, "Table %s:" % table
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
36 try:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
37 col_query = 'SELECT * FROM %s LIMIT 0' % table
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
38 cur = conn.cursor().execute(col_query)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
39 cols = [col[0] for col in cur.description]
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
40 print >> sys.stderr, " Columns: %s" % cols
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
41 except Exception, exc:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
42 print >> sys.stderr, "Error: %s" % exc
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
43 except Exception, exc:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
44 print >> sys.stderr, "Error: %s" % exc
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
45 exit(1)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
46 #if not sqlite.is_read_only_query(options.query):
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
47 # print >> sys.stderr, "Error: Must be a read only query"
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
48 # exit(2)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
49 try:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
50 conn = sqlite.connect(options.sqlitedb)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
51 cur = conn.cursor()
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
52 results = cur.execute(options.query)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
53 if not options.no_header:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
54 outputFile.write("#%s\n" % '\t'.join([str(col[0]) for col in cur.description]))
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
55 # yield [col[0] for col in cur.description]
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
56 for i,row in enumerate(results):
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
57 # yield [val for val in row]
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
58 outputFile.write("%s\n" % '\t'.join([str(val) for val in row]))
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
59 except Exception, exc:
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
60 print >> sys.stderr, "Error: %s" % exc
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
61 exit(1)
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
62
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
63 if __name__ == "__main__": __main__()
30a37dd92ccd Uploaded
jjohnson
parents:
diff changeset
64