view sqlex/model.py @ 2:63a75d318b06

stub a model
author Jeff Hammel <k0scist@gmail.com>
date Sat, 01 Apr 2017 08:44:42 -0700
parents
children 5f1e1ac96aa7
line wrap: on
line source

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