Python Forum

Full Version: function for SQLite query not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In the following program I'm trying to get my sqlite connection and the cursor to be returned in a tuple and used from my main.py file. I'm not getting any errors but when I call check_table from main.py all I get is press any key to continue, I don't get the printout of the table structure that I'm wanting. Note that this code was working before I placed it in it's own function and tried to make it flexible to be called upon for any table. The directory and file creation part of this program is also working fine.

main.py
import db2

database = db2.setup_db()

db2.check_table(database[1], "Books")

input("Press any key to continue")
db2.py
import sqlite3
import os

def setup_db():
    directory = "C:\\Users\\me\\librarydb"
    dbfile = "C:\\Users\\me\\librarydb\\booksdb"
    #connections = ()

    if not os.path.exists(directory):
        print("directory does not exist")
        os.mkdir(directory)

        if not os.path.exists(dbfile):
            build_db()
            
        conn = sqlite3.connect(r"C:\\Users\\me\\librarydb\\booksdb")
        c = conn.cursor()

    conn = sqlite3.connect(r"C:\\Users\\me\\librarydb\\booksdb")
    c = conn.cursor()

    return (conn, c)
    
def check_table(connection, table):
    #conn = sqlite3.connect(r"C:\\Users\\me\\librarydb\\booksdb")
    #c = conn.cursor()

    meta = connection.execute("PRAGMA table_info(%s)" % (table))
    for r in meta:
        print(r)

def build_db():
    conn = sqlite3.connect(r"C:\\Users\\me\\librarydb\\booksdb")
    c = conn.cursor()
    c.execute('''CREATE TABLE Books
                (book_id int,
                author_id int,
                book_title varchar(100),
                genre varchar(50),
                type varchar(30),
                cover char,
                price real)''')

    conn.commit()
    conn.close()
because setup_db() is a function, by the time you get to:
db2.check_table(database[1], "Books")
you cursor and connection are already out of scope.
You really need to make db2 a class, with the setup_db in a __init__ method

like this (I couldn't test, so there may (probably will) be errors):
main.py:
import db2

database = db2.db2()

database.check_table("Books")

input("Press any key to continue")
db2.py
import sqlite3
import os

class db2:
    def __init__(self):
        directory = "C:\\Users\\me\\librarydb"
        dbfile = "C:\\Users\\me\\librarydb\\booksdb"
        # connections = ()

        if not os.path.exists(directory):
            print("directory does not exist")
            os.mkdir(directory)

            if not os.path.exists(dbfile):
                self.build_db()

            conn = sqlite3.connect(r"C:\\Users\\me\\librarydb\\booksdb")
            c = conn.cursor()

        self.conn = sqlite3.connect(r"C:\\Users\\me\\librarydb\\booksdb")
        self.cur = self.conn.cursor()


    def check_table(self, table):
        # conn = sqlite3.connect(r"C:\\Users\\me\\librarydb\\booksdb")
        # c = conn.cursor()

        meta = self.cur.execute("PRAGMA table_info(%s)" % (table))
        for r in meta:
            print(r)


    def build_db(self):
        self.conn = sqlite3.connect(r"C:\\Users\\me\\librarydb\\booksdb")
        self.c = self.conn.cursor()
        self.c.execute('''CREATE TABLE Books
                    (book_id int,
                    author_id int,
                    book_title varchar(100),
                    genre varchar(50),
                    type varchar(30),
                    cover char,
                    price real)''')

    def close_db(self):
        self.conn.commit()
        self.conn.close()