0
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)

1 Answers1

1

So, you fetch the result, but never do anything with it, then return the spent cursor...

def fetch(self):
    self.c.fetchall() # this returns a list of your data 
    self.conn.commit()
    return self.c

It's also not clear what you are committing in the function

Compare to your other code,

rows = c.fetchall()

So you should only need

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

Then call it

tables = dbmgr.fetch()

Or, alternatively

tables = dbmgr.query(query).fetchall()

Note that you are making an empty list, then adding elements of a list to that list. You can shortcut that process by assigning tables directly

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245