comparison query_tabular.py @ 2:ffa5e34a55c1

Uploaded
author jjohnson
date Sun, 04 Oct 2015 10:52:21 -0400
parents 24f0911f75ee
children 125fc5d123b3 afdbc7198353
comparison
equal deleted inserted replaced
1:24f0911f75ee 2:ffa5e34a55c1
109 #Parse Command Line 109 #Parse Command Line
110 parser = optparse.OptionParser() 110 parser = optparse.OptionParser()
111 parser.add_option( '-s', '--sqlitedb', dest='sqlitedb', default=None, help='The SQLite Database' ) 111 parser.add_option( '-s', '--sqlitedb', dest='sqlitedb', default=None, help='The SQLite Database' )
112 parser.add_option( '-t', '--table', dest='tables', action="append", default=[], help='Tabular file: file_path[=table_name[:column_name,...]' ) 112 parser.add_option( '-t', '--table', dest='tables', action="append", default=[], help='Tabular file: file_path[=table_name[:column_name,...]' )
113 parser.add_option( '-q', '--query', dest='query', default=None, help='SQL query' ) 113 parser.add_option( '-q', '--query', dest='query', default=None, help='SQL query' )
114 parser.add_option( '-Q', '--query_file', dest='query_file', default=None, help='SQL query file' )
114 parser.add_option( '-n', '--no_header', dest='no_header', action='store_true', default=False, help='Include a column headers line' ) 115 parser.add_option( '-n', '--no_header', dest='no_header', action='store_true', default=False, help='Include a column headers line' )
115 parser.add_option( '-o', '--output', dest='output', default=None, help='Output file for query results' ) 116 parser.add_option( '-o', '--output', dest='output', default=None, help='Output file for query results' )
116 (options, args) = parser.parse_args() 117 (options, args) = parser.parse_args()
117 118
118 # oprn sqlite connection 119 # oprn sqlite connection
141 column_names = names[1] 142 column_names = names[1]
142 # print >> sys.stdout, '%s %s' % (table_name, path) 143 # print >> sys.stdout, '%s %s' % (table_name, path)
143 create_table(conn,path,table_name,column_names=column_names) 144 create_table(conn,path,table_name,column_names=column_names)
144 conn.close() 145 conn.close()
145 146
146 if (options.query is None): 147 query = None
148 if (options.query_file != None):
149 with open(options.query_file,'r') as fh:
150 query = ''
151 for line in fh:
152 query += line
153 elif (options.query != None):
154 query = options.query
155
156 if (query is None):
147 try: 157 try:
148 conn = sqlite.connect(options.sqlitedb) 158 conn = sqlite.connect(options.sqlitedb)
149 c = conn.cursor() 159 c = conn.cursor()
150 tables_query = "SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name" 160 tables_query = "SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name"
151 rslt = c.execute(tables_query).fetchall() 161 rslt = c.execute(tables_query).fetchall()
159 except Exception, exc: 169 except Exception, exc:
160 print >> sys.stderr, "Error: %s" % exc 170 print >> sys.stderr, "Error: %s" % exc
161 except Exception, exc: 171 except Exception, exc:
162 print >> sys.stderr, "Error: %s" % exc 172 print >> sys.stderr, "Error: %s" % exc
163 exit(0) 173 exit(0)
164 #if not sqlite.is_read_only_query(options.query): 174 #if not sqlite.is_read_only_query(query):
165 # print >> sys.stderr, "Error: Must be a read only query" 175 # print >> sys.stderr, "Error: Must be a read only query"
166 # exit(2) 176 # exit(2)
167 try: 177 try:
168 conn = sqlite.connect(options.sqlitedb) 178 conn = sqlite.connect(options.sqlitedb)
169 cur = conn.cursor() 179 cur = conn.cursor()
170 results = cur.execute(options.query) 180 results = cur.execute(query)
171 if not options.no_header: 181 if not options.no_header:
172 outputFile.write("#%s\n" % '\t'.join([str(col[0]) for col in cur.description])) 182 outputFile.write("#%s\n" % '\t'.join([str(col[0]) for col in cur.description]))
173 # yield [col[0] for col in cur.description] 183 # yield [col[0] for col in cur.description]
174 for i,row in enumerate(results): 184 for i,row in enumerate(results):
175 # yield [val for val in row] 185 # yield [val for val in row]