mardi 26 avril 2016

Receiving table information in python/sql to print to console

class DatabaseManager(object): ##Ignore the indentation##
def __init__(self, db):
    self.conn = sqlite3.connect(db)
    self.conn.execute('pragma foreign_keys = on')
    self.conn.commit()
    self.c = self.conn.cursor()


def query_params(self, arg, params=None):
    self.c.execute(arg, params)
    self.conn.commit()
    return self.c

def query(self, arg):
    self.c.execute(arg)
    self.conn.commit()
    return self.c

def fetch(self):
    self.c.fetchall()
    self.conn.commit()
    return self.c

def __del__(self):
    self.conn.close()

dbmgr = DatabaseManager("connect.db")

Other code .... .... .... and then:

def read_from_db():

    tables = []
    query = "SELECT name FROM sqlite_master WHERE type='table'"
    dbmgr.query(query)
    rows = dbmgr.fetch()
    for row in rows:
        tables.append(row)
    print (tables)

I am not able to figure out how to view the tables in my database, they are real as I can see them in my sqlite manager. This used to work when I did not have the class and just did:

    def read_from_db():

    tables = []
    c.execute("SELECT name FROM sqlite_master WHERE type='table'")
    rows = c.fetchall()
    for row in rows:
        tables.append(row)
    print (tables)

Aucun commentaire:

Enregistrer un commentaire