Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a custom tkinter app and allow the user to update their data. Even thought the message popup the Record Updated, but the SQLite database seem did not change. Where am I go wrong? How am I fix it?

What I have tried:

Python
def update():
    selected_item = row_selector.get()[0][0]
    print(selected_item)
    my_name = name.get()
    print(my_name)
    my_class = class1.get()
    print(my_class)
    my_mark = mark1.get()
    print(my_mark)
    my_gender = gender.get()
    print(my_gender)
    my_address = address.get("1.0", "end")
    print(my_address)
    my_hostel = hostel.get()
    print(my_hostel)

    try:  
        sql = text("""UPDATE student_address SET name = :name, class = :class,
                mark = :mark, gender = :gender, address = :address, hostel = :hostel
                WHERE id = :id""")
        
        with engine.connect() as my_conn:
            my_cursor = my_conn.execute(sql, 
                {"name": my_name,
                 "class": my_class,
                 "mark": my_mark,
                 "gender": my_gender,
                 "address": my_address,
                 "hostel": my_hostel,
                 "id": selected_item})


        CTkMessagebox(title="Update", message="Record Updated!")
        # edited_row = table.edit_row(selected_item, *args)
        # print(edited_row)
        my_conn.commit()

    except SQLAlchemyError as e:
        print(f"An error occurred: {e}")
        CTkMessagebox(title="Error", 
                      message="Please select a record to update", 
                      icon="cancel")

btn6_update = ctk.CTkButton(bottom_frame,
    fg_color='purple',
    state='disabled',
    font=font1,
    hover_color="black",
    text_color='white',
    text="Update",
    command= update)
btn6_update.grid(row=5,column=6, padx=20)
Posted
Comments
Richard MacCutchan 27-Apr-24 6:12am    
You should check the value of rowcount on the returned cursor object, to see if the statement succeeded. Never assume it succeeds because it did not raise an exception.

1 solution

The most likely solution is that the WHERE clause failed: either use the debugger or add logging code to find out exactly what is in selected_item when that code executes, and compare it against the actual DB content. Unless they match exactly no rows will be updated.

Sorry, but we can't do that for you: we have no access to your code while it is running, or to your DB at any time!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900