# HG changeset patch # User Jeff Hammel # Date 1458070121 25200 # Node ID 7f92e207830b21bcfc32d028ffdb72891141ec36 # Parent 4b51bcbcdb156f48bcb2489f58a2e1adedf47649 basic functionality diff -r 4b51bcbcdb15 -r 7f92e207830b sqlitex/api.py --- a/sqlitex/api.py Tue Mar 15 11:29:22 2016 -0700 +++ b/sqlitex/api.py Tue Mar 15 12:28:41 2016 -0700 @@ -8,7 +8,7 @@ import os import sqlite3 import sys - +from StringIO import StringIO class SQLitEx(object): @@ -17,6 +17,45 @@ if not os.path.isfile(self.db_file): raise AssertionError("Not a file: '{0}'".format(self.db_file)) + def __call__(self, query): + con = sqlite3.connect(self.db_file) + cursor = con.cursor() + cursor.execute(query) + return cursor.fetchall() + + def tables(self): + """ + return a list of tables; see + http://stackoverflow.com/questions/305378/get-list-of-tables-db-schema-dump-etc-in-sqlite-databases + """ + tables = self("SELECT name FROM sqlite_master WHERE type='table';") + tables = [i[0] for i in tables] + return tables + + def contents(self, table): + rows = self("SELECT * FROM {table};".format(table=table)) + return rows + + def describe(self, table): + con = sqlite3.connect(self.db_file) + cursor = con.cursor() + cursor.execute("SELECT * FROM {table} LIMIT 1;".format(table=table)) + header = [i[0] for i in cursor.description] + return header + +def rows2csv(rows, fp=None): + """convert rows to csv""" + + return_string = False + if fp is None: + return_string = True + fp = StringIO() + writer = csv.writer(fp) + writer.writerows(rows) + fp.flush() + if return_string: + return fp.getvalue() + def main(args=sys.argv[1:]): """CLI""" @@ -33,5 +72,15 @@ # instantiate api api = SQLitEx(options.db_file) + if options.table: + rows = api.contents(options.table) + header = api.describe(options.table) + rows.insert(0, header) + print (rows2csv(rows)) + else: + # list tables + tables = api.tables() + print ('\n'.join(tables)) + if __name__ == '__main__': main()