Mercurial > repos > jjohnson > sqlite_to_tabular
comparison sqlite_to_tabular.py @ 2:bc50a7b7f246 draft
Uploaded
| author | jjohnson |
|---|---|
| date | Sun, 04 Oct 2015 10:51:12 -0400 |
| parents | 30a37dd92ccd |
| children | f079ea3884b3 |
comparison
equal
deleted
inserted
replaced
| 1:1819a06a01eb | 2:bc50a7b7f246 |
|---|---|
| 8 def __main__(): | 8 def __main__(): |
| 9 #Parse Command Line | 9 #Parse Command Line |
| 10 parser = optparse.OptionParser() | 10 parser = optparse.OptionParser() |
| 11 parser.add_option( '-s', '--sqlitedb', dest='sqlitedb', default=None, help='The SQLite Database' ) | 11 parser.add_option( '-s', '--sqlitedb', dest='sqlitedb', default=None, help='The SQLite Database' ) |
| 12 parser.add_option( '-q', '--query', dest='query', default=None, help='SQL query' ) | 12 parser.add_option( '-q', '--query', dest='query', default=None, help='SQL query' ) |
| 13 parser.add_option( '-Q', '--query_file', dest='query_file', default=None, help='SQL query file' ) | |
| 13 parser.add_option( '-n', '--no_header', dest='no_header', action='store_true', default=False, help='Include a column headers line' ) | 14 parser.add_option( '-n', '--no_header', dest='no_header', action='store_true', default=False, help='Include a column headers line' ) |
| 14 parser.add_option( '-o', '--output', dest='output', default=None, help='Output file for query results' ) | 15 parser.add_option( '-o', '--output', dest='output', default=None, help='Output file for query results' ) |
| 15 (options, args) = parser.parse_args() | 16 (options, args) = parser.parse_args() |
| 16 | 17 |
| 17 # determine output destination | 18 # determine output destination |
| 23 print >> sys.stderr, "failed: %s" % e | 24 print >> sys.stderr, "failed: %s" % e |
| 24 exit(3) | 25 exit(3) |
| 25 else: | 26 else: |
| 26 outputFile = sys.stdout | 27 outputFile = sys.stdout |
| 27 | 28 |
| 28 if (options.query is None): | 29 query = None |
| 30 if (options.query_file != None): | |
| 31 with open(options.query_file,'r') as fh: | |
| 32 query = '' | |
| 33 for line in fh: | |
| 34 query += line | |
| 35 elif (options.query != None): | |
| 36 query = options.query | |
| 37 | |
| 38 if (query is None): | |
| 29 try: | 39 try: |
| 30 conn = sqlite.connect(options.sqlitedb) | 40 conn = sqlite.connect(options.sqlitedb) |
| 31 c = conn.cursor() | 41 c = conn.cursor() |
| 32 tables_query = "SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name" | 42 tables_query = "SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name" |
| 33 rslt = c.execute(tables_query).fetchall() | 43 rslt = c.execute(tables_query).fetchall() |
| 40 print >> sys.stderr, " Columns: %s" % cols | 50 print >> sys.stderr, " Columns: %s" % cols |
| 41 except Exception, exc: | 51 except Exception, exc: |
| 42 print >> sys.stderr, "Error: %s" % exc | 52 print >> sys.stderr, "Error: %s" % exc |
| 43 except Exception, exc: | 53 except Exception, exc: |
| 44 print >> sys.stderr, "Error: %s" % exc | 54 print >> sys.stderr, "Error: %s" % exc |
| 45 exit(1) | 55 exit(0) |
| 46 #if not sqlite.is_read_only_query(options.query): | 56 #if not sqlite.is_read_only_query(query): |
| 47 # print >> sys.stderr, "Error: Must be a read only query" | 57 # print >> sys.stderr, "Error: Must be a read only query" |
| 48 # exit(2) | 58 # exit(2) |
| 49 try: | 59 try: |
| 50 conn = sqlite.connect(options.sqlitedb) | 60 conn = sqlite.connect(options.sqlitedb) |
| 51 cur = conn.cursor() | 61 cur = conn.cursor() |
| 52 results = cur.execute(options.query) | 62 results = cur.execute(query) |
| 53 if not options.no_header: | 63 if not options.no_header: |
| 54 outputFile.write("#%s\n" % '\t'.join([str(col[0]) for col in cur.description])) | 64 outputFile.write("#%s\n" % '\t'.join([str(col[0]) for col in cur.description])) |
| 55 # yield [col[0] for col in cur.description] | 65 # yield [col[0] for col in cur.description] |
| 56 for i,row in enumerate(results): | 66 for i,row in enumerate(results): |
| 57 # yield [val for val in row] | 67 # yield [val for val in row] |
