Help saving data to database

Thanks. Did that. Where is _W1 defined? (error message)

I did have it in my old program but in a whole def;

def main(*args):
‘’‘Main entry point for the application.’‘’
global root
root = tk.Tk()
root.protocol( ‘WM_DELETE_WINDOW’ , root.destroy)
# Creates a toplevel widget.
global _top1, _w1
_top1 = root
_w1 = BooksLibrary.BooksLibrary(_top1)
root.mainloop()

You set _w1 (not _W1, case matters) in main and made it global, so it exists in that module as a whole.

1 Like

Here is an updated version. I had to swap the order of the classes because the methods have to make use of self. If you run the app, and enter miscellaneous values into the fields, and then click the Add button, the values will be reflected by the print statements. I am using the on_btnAddClick method because I am assuming this is the button that you use to enter the values into the database.
Once you have verified that the information is accurately being read, you can move on to the next step. This is what I mean by adding a functionality, testing it, verifying that it is functioning per requirements, and then moving on to the next one. But you have to test and verify along the way no matter how insignificant you think that functionality that you’re adding might be.

import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk


class BooksLibraryGUI(tk.Tk):# , ButtonCallBacks):

    def __init__(self, top=None):

        super().__init__()

        self.geometry("400x390+572+464")
        self.minsize(72, 15)
        self.maxsize(1600, 847)
        self.resizable(1,  1)
        self.title("Books Library")
        self.configure(background="wheat")
        self.configure(highlightbackground="wheat")
        self.configure(highlightcolor="black")

        # Change widget icon (you will have to edit actual path of image)
        ico = Image.open('C:/Desktop/book_image.jpg')
        photo = ImageTk.PhotoImage(ico)
        self.wm_iconphoto(False, photo)

        self.top = top
        self.combobox = tk.StringVar()

        # _style_code()
        self.lblYearPub = ttk.Label(self.top)
        self.lblYearPub.place(relx=0.053, rely=0.441, height=21, width=115)
        self.lblYearPub.configure(font="-family {.AppleSystemUIFont} -size 13")
        self.lblYearPub.configure(relief="flat")
        self.lblYearPub.configure(text='''Year Published''')
        self.lblYearPub.configure(compound='left')

        self.eAuthor = ttk.Entry(self.top)
        self.eAuthor.place(relx=0.361, rely=0.359, relheight=0.059
                , relwidth=0.481)
        self.eAuthor.configure(font="-family {.AppleSystemUIFont} -size 13")
        self.eAuthor.configure(cursor="ibeam")

        self.eYearPub = ttk.Entry(self.top)
        self.eYearPub.place(relx=0.361, rely=0.441, relheight=0.062
                , relwidth=0.208)
        self.eYearPub.configure(font="-family {.AppleSystemUIFont} -size 13")
        self.eYearPub.configure(cursor="ibeam")

        self.eTitle = ttk.Entry(self.top)
        self.eTitle.place(relx=0.361, rely=0.274, relheight=0.062
                , relwidth=0.481)
        self.eTitle.configure(font="-family {.AppleSystemUIFont} -size 13")
        self.eTitle.configure(cursor="ibeam")

        self.lblTitle = ttk.Label(self.top)
        self.lblTitle.place(relx=0.053, rely=0.274, height=22, width=38)
        self.lblTitle.configure(font="-family {.AppleSystemUIFont} -size 13")
        self.lblTitle.configure(relief="flat")
        self.lblTitle.configure(text='''Title''')
        self.lblTitle.configure(compound='left')

        self.btnExit = ttk.Button(self.top)
        self.btnExit.place(relx=0.542, rely=0.826, height=26, width=90)
        self.btnExit.configure(command=self.on_btnExitClick)
        self.btnExit.configure(text='''Exit''')
        self.btnExit.configure(compound='left')

        self.btnDelete = ttk.Button(self.top)
        self.btnDelete.place(relx=0.694, rely=0.674, height=26, width=70)
        self.btnDelete.configure(command=self.on_btnDeleteClick)
        self.btnDelete.configure(text='''Delete''')
        self.btnDelete.configure(compound='left')

        self.lblCategory = ttk.Label(self.top)
        self.lblCategory.place(relx=0.053, rely=0.192, height=22, width=75)
        self.lblCategory.configure(font="-family {.AppleSystemUIFont} -size 13")
        self.lblCategory.configure(relief="flat")
        self.lblCategory.configure(text='''Category''')
        self.lblCategory.configure(compound='left')

        self.btnEdit = ttk.Button(self.top)
        self.btnEdit.place(relx=0.278, rely=0.674, height=26, width=65)
        self.btnEdit.configure(command=self.on_btnEditClick)
        self.btnEdit.configure(text='''Edit''')
        self.btnEdit.configure(compound='left')

        self.btnUpdate = ttk.Button(self.top)
        self.btnUpdate.place(relx=0.472, rely=0.674, height=26, width=75)
        self.btnUpdate.configure(command=self.on_btnUpdateClick)
        self.btnUpdate.configure(text='''Update''')
        self.btnUpdate.configure(compound='left')

        self.btnAdd = ttk.Button(self.top)
        self.btnAdd.place(relx=0.083, rely=0.674, height=26, width=65)
        self.btnAdd.configure(command=self.on_btnAddClick)
        self.btnAdd.configure(text='''Add''')
        self.btnAdd.configure(compound='left')

        self.lblComments = ttk.Label(self.top)
        self.lblComments.place(relx=0.053, rely=0.523, height=21, width=80)
        self.lblComments.configure(font="-family {.AppleSystemUIFont} -size 13")
        self.lblComments.configure(relief="flat")
        self.lblComments.configure(text='''Comments''')
        self.lblComments.configure(compound='left')

        self.cmbCategory = ttk.Combobox(self.top, textvariable=self.combobox)
        self.cmbCategory.place(relx=0.361, rely=0.192, relheight=0.062, relwidth=0.517)
        self.value_list = ['Horror','Historical','Literary','Mystery','Romance','Science','Fiction','Thriller',]
        self.cmbCategory.configure(values=self.value_list)
        self.cmbCategory.configure(font="-family {.AppleSystemUIFont} -size 13")
        self.cmbCategory.configure(textvariable=self.combobox)

        self.eRecNo = ttk.Entry(self.top)
        self.eRecNo.place(relx=0.361, rely=0.11, relheight=0.062, relwidth=0.208)

        self.eRecNo.configure(font="-family {.AppleSystemUIFont} -size 13")
        self.eRecNo.configure(cursor="ibeam")

        self.lblAuthor = ttk.Label(self.top)
        self.lblAuthor.place(relx=0.053, rely=0.359, height=21, width=52)
        self.lblAuthor.configure(font="-family {.AppleSystemUIFont} -size 13")
        self.lblAuthor.configure(relief="flat")
        self.lblAuthor.configure(text='''Author''')
        self.lblAuthor.configure(compound='left')

        self.lblRecNo = ttk.Label(self.top)
        self.lblRecNo.place(relx=0.053, rely=0.11, height=21, width=57)
        self.lblRecNo.configure(font="-family {.AppleSystemUIFont} -size 13")
        self.lblRecNo.configure(relief="flat")
        self.lblRecNo.configure(text='''Record #''')
        self.lblRecNo.configure(compound='left')

        self.eComments = ttk.Entry(self.top)
        self.eComments.place(relx=0.361, rely=0.523, relheight=0.062
                , relwidth=0.481)
        self.eComments.configure(font="-family {.AppleSystemUIFont} -size 13")
        self.eComments.configure(cursor="ibeam")

class LibraryApp(BooksLibraryGUI):
    

    def on_btnAddClick(self, *args):
        
        print('Entered on_btnAddClick')
        print('\nValues entered in the Library GUI:')
        print(self.eRecNo.get())     # Record Number
        print(self.combobox.get())   # Category
        print(self.eTitle.get())     # Book Title
        print(self.eAuthor.get())    # Author
        print(self.eYearPub.get())   # Year published
        print(self.eComments.get())  # Comments


    def on_btnDeleteClick(self, *args):
        print('Entered on_btnDeleteClick')

    def on_btnEditClick(self, *args):
        print('Entered on_btnEditClick')

    def on_btnExitClick(self, *args):
        print('Entered on_btnExitClick')

    def on_btnUpdateClick(self, *args):
        print('Entered on_btnUpdateClick')

if __name__ == "__main__":

    app = LibraryApp()
    app.title("Book Library App")
    app.mainloop()

It does help if you understand what you’re doing. But it might not help if you’re attempting to create a complex application just to get to the finish line. If you do not understand the intermediate steps, in the long run, it might not help much.

I suggest building it one step at a time so that you fully understand what it is you’re developing.

Wow Paul. Thank you so much for all that code and input. I now unnderstand the basic process of getting the information from the form into the print statement.
I can now move on (just for this one button) to getting it into the database. And then work it through all the buttons. That is such a very big help to me to have gone this far in my learning and I am grateful to you for the time you have given me.
I have to stop now so thanks again. I find it all quite exciting to watch it unfold.

1 Like

I have now put the field values into variables which a print statement verifies works. Getting this into the database is my problem as whatever I do does not seem to work. I have the following liines in my app:

curs.execute("INSERT INTO Books(Record,Category,Title,Author,Year_Published,Comments)\
                     VALUES(NULL, ?, ?, ?, ?, ?),(vRec,vCat,vTit,vAuth,vYear,vComments)");

This produces an error message “No such column: vRec”

So I revised the code:

curs.execute("INSERT INTO Books(Record,Category,Title,Author,Year_Published,Comments)\
         VALUES(NULL,?, ?, ?, ?, ?), (Record,Category,Title,Author,Year_Published,Comments)");

I don’t know why but I got the error message “No such column: Record” again. I looked up my table DB Schema and Record is the name of the first field.
I’m not sure how to get this right.

Your original code had:

curs.execute("INSERT INTO Books(Category,Title,Author,Year_Purchased,\
                 Comments) VALUES(?, ?, ?,?,?)", (vCat,vTit,vAuth,vYear,\
                                                  vComments) );  

and your new code has:

curs.execute("INSERT INTO Books(Record,Category,Title,Author,Year_Published,Comments)\
         VALUES(NULL,?, ?, ?, ?, ?), (Record,Category,Title,Author,Year_Published,Comments)");

Do you see the difference?

(Also, you don’t need the semicolon at the end of the line.)

1 Like

Hello Matthew

Yes, now I see it. Thank you very much. That works perfectly. So, again, I would like to thank you for your help. I can now make more progress with the rest of my program.

Bestregards
Neil

1 Like