# HG changeset patch # User Jeff Hammel # Date 1491061482 25200 # Node ID 63a75d318b06d20d1ef9d93a69ed53eb0ad03bfd # Parent 1cfdb859f9d2ee6dc9caaf9d6ab9af082091463d stub a model diff -r 1cfdb859f9d2 -r 63a75d318b06 sqlex/main.py --- a/sqlex/main.py Fri Mar 31 20:31:31 2017 -0700 +++ b/sqlex/main.py Sat Apr 01 08:44:42 2017 -0700 @@ -47,6 +47,7 @@ except Exception as e: self.error("Could not open '{}': {}".format(options.db, e)) + def main(args=sys.argv[1:]): """CLI""" diff -r 1cfdb859f9d2 -r 63a75d318b06 sqlex/model.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sqlex/model.py Sat Apr 01 08:44:42 2017 -0700 @@ -0,0 +1,30 @@ +import sqlite3 + +class SQLEx(object): + """ + sqlite model + https://docs.python.org/2/library/sqlite3.html + """ + + def __init__(self, db): + self.db = db + self.conn = sqlite3.connect(self.db) + + def __call__(self, sql, *args): + c = conn.cursor() + c.execute(sql, args) + c.commit() + + def __del__(self): + self.conn.close() + + def tables(self): + """ + returns table names in database + """ + # http://stackoverflow.com/questions/82875/how-to-list-the-tables-in-an-sqlite-database-file-that-was-opened-with-attach + + sql = "SELECT name FROM dbname.sqlite_master WHERE type='table';" + tables = self(sql) + return tables +