I am trying to create a program in tkinter which would implement PostGreSQL databases. All goes well until the issue of modifying data. The image below shows how it currently looks like
I wanted to make it so the modification process is composed of two subroutines: One takes the selected object and “moves it” to the inserts on left, so I can modify them, after pressing the button the object gets modified and all is good. Here is mine “attempt”:
def MODIFY_USER():
connect, cursor = connection_to_db()
i = listbox_object_list.index(ACTIVE)
entry_name_unit.delete(0, END)
entry_adress_unit.delete(0, END)
entry_phone_number_u.delete(0, END)
# Fetch user data from PostgreSQL
cursor.execute('SELECT * FROM units WHERE "ID_UNIT" = %s', (i + 1,))
user_data = cursor.fetchone()
entry_name_unit.insert(0, user_data[1])
entry_adress_unit.insert(0, user_data[2])
entry_phone_number_u.insert(0, user_data[3])
button_add_object.config(text="Save", command=lambda: UPDATE_USER(i))
def UPDATE_USER(i: int):
connect, cursor = connection_to_db()
i = listbox_object_list.index(ACTIVE)
cursor.execute("UPDATE units SET name = %s, adress = %s, phonenumber = %s WHERE 'ID_UNIT' = %s",
(entry_name_unit.get(), entry_surname.get(), entry_phone_number_u.get(), i + 1))
connect.commit()
button_add_object.config(text="Add object", command=ADD_UNIT)
entry_name_unit.delete(0, END)
entry_adress_unit.delete(0, END)
entry_phone_number_u.delete(0, END)
Which itself is an attempt of morphing this to use PostGreSQL:
def MODIFY_USER():
i = listbox_object_list.index(ACTIVE)
entry_name.delete(0, END)
entry_surname.delete(0, END)
entry_city.delete(0, END)
entry_posts.delete(0, END)
entry_name.insert(0, Users[i].name)
entry_surname.insert(0, Users[i].surname)
entry_city.insert(0, Users[i].location)
entry_posts.insert(0, Users[i].posts)
button_add_object.config(text="Zapisz zmiany", command=lambda:UPDATE_USER(i))
def UPDATE_USER(i:int):
i = listbox_object_list.index(ACTIVE)
Users[i].name = entry_name.get()
Users[i].surname= entry_surname.get()
Users[i].location= entry_city.get()
Users[i].posts = entry_posts.get()
button_add_object.config(text="Add object", command=ADD_USER)
entry_name.delete(0, END)
entry_surname.delete(0, END)
entry_city.delete(0, END)
entry_posts.delete(0, END)
USER_LIST()
The most common kind of error is this one:
line 200, in MODIFY_USER
entry_name_unit.insert(0, user_data[1])
~~~~~~~~~^^^
TypeError: 'NoneType' object is not subscriptable
What should I do? Scrap the code in its entirety and start anew?