changeset 2:63a75d318b06

stub a model
author Jeff Hammel <k0scist@gmail.com>
date Sat, 01 Apr 2017 08:44:42 -0700
parents 1cfdb859f9d2
children 5f1e1ac96aa7
files sqlex/main.py sqlex/model.py
diffstat 2 files changed, 31 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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"""
 
--- /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
+