Python Forum
function for SQLite query not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function for SQLite query not working
#1
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()
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel isnt working properly after python function is started IchNar 2 350 May-01-2024, 06:43 PM
Last Post: IchNar
Shocked kindly support with this dropna function not working gheevarpaulosejobs 2 730 Jul-24-2023, 03:41 PM
Last Post: deanhystad
Exclamation Function Not Working Alivegamer 7 1,938 Jul-19-2022, 01:03 PM
Last Post: deanhystad
  MSSQL query not working in Python kat35601 0 953 Apr-12-2022, 06:44 PM
Last Post: kat35601
  Building SQLite Query OogieM 10 3,113 Mar-16-2022, 11:59 AM
Last Post: OogieM
  Query in sqlite database frewil 2 1,605 Feb-06-2022, 05:35 PM
Last Post: frewil
  try function working on PC but not raspberry pi AnotherSam 1 1,576 Oct-11-2021, 04:51 AM
Last Post: bowlofred
  Write SQLite query result to file hjk6734 1 1,972 May-27-2020, 12:17 PM
Last Post: menator01
  Input() function not working in VS Code darpInd 7 13,518 Feb-17-2020, 03:28 PM
Last Post: snippsat
  Unrelated new function causing existing working function to fail Nick_G 2 2,304 Jan-27-2020, 07:21 PM
Last Post: Nick_G

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020