May-16-2018, 03:57 PM
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
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()