Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
753 views
in Technique[技术] by (71.8m points)

database - Creating an Update Record Function using Python and SQLite3

I am currently working on a coursework project for school and it is a database system with a user interface using Tkinter, Python and SQLite3. I have made a form to add, delete, update and search for customers. The update function is not producing any errors, however, the changes are not being made to the .db file. Could anyone please have a look and maybe tell me where I have went wrong? I will attach photos of the user interface and the code for the function. I'm not used to using StackOverflow, so please let me know if I have left anything out or if you need more information. Thank you in advance.

    def UpdateCustomer(self):
        customerid = self.CustomerEntry.get();
        town = self.TownEntry.get();
        postcode = self.PostcodeEntry.get();
        with sqlite3.connect("LeeOpt.db") as db:
            cursor = db.cursor()
            update_customer = ('''UPDATE Customer 
            SET 
            Town = ?,
            Postcode = ?
            WHERE CustomerID = ?
            ''')
            db.commit()

            cursor.execute(update_customer,[(customerid),(town),(postcode)])
            tkinter.messagebox.showinfo("Notification","Customer record updated successfully")
            self.ClearEntries()

enter image description here

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You must pass the parameter values that will replace the ? placeholdrs in the exact same order as they are in the sql statement:

cursor.execute(update_customer, (town, postcode, customerid))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...