Python Forum
[PyQt] How to update selected data in combobox displaying DB data?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] How to update selected data in combobox displaying DB data?
#1
I have buttons in the combobox where I view DB data so I can edit, delete, and update the data I selected. When I press the edit button of a db data I selected in Combobox, I transfer it to qlineedit, but when you make changes and click the update button, all the database data is updated instead of the db data I selected. As you can see in my code example below, this is normal. What I want to do is just update the data I've selected.
def update(self):
    con = sqlite3.connect('DataBase.db')
    cur = con.cursor()
    message = QMessageBox.question(self, "SAMPLE", "Are you sure you want to update the record??", QMessageBox.Yes | QMessageBox.No)
    if message == QMessageBox.Yes: 
        try:
            select = self.ui.combo_SampleName.currentText()
           SName = self.ui.line_SampleDbUpdateAdd.text()
            cur.execute("UPDATE SampledB SET NAME=?", [SName])
            con.commit()
            self.ui.combo_SampleName.clear()
            sampleComboList(self)
            self.ui.label_SampleCenteRightSpace.setText("REGISTRATION UPDATED")
        except Exception as Error:
            self.ui.label_SampleCenteRightSpace.setText("AN UNEXPECTED ERROR OCCURRED UPDATE FAILED")
    else:
        self.ui.label_SampleCenteRightSpace.setText("REGISTRATION UPDATE CANCELED")
I do not have the id of the data in the combobox, I cannot assign a variable and WHERE Id=? I can't follow a path. However, id is automatically assigned when registering on Db. What I'm trying to do is just select a data from the Combobox and update only that data. I've done so much research but couldn't get any results other than confusion.

etc :
sampleId = select
cur.execute("UPDATE SampledB SET NAME=? WHERE ID=?", (SName, sampleId))

I want to know how I should make changes to my code to get the result I want. I would really appreciate it if you could enlighten me on this matter. I am trying to learn this business with my own curiosity and improve myself. On top of that, I have not studied in any school on this profession, so the question I ask is very easy for you due to my insufficient knowledge, but it is just as difficult for me. My final point in my current research is to use SELECT in the UPDATE command. I don't know if this way is correct either, so I'm trying to research it and learn how to use it. I don't know if it will work once I find out.
Reply
#2
Have you tried writing a program that connects to the database, does a query and prints the result? Not a GUI program, just a console program. I would start with that. Once I had that working I would write another console program that connects to the data base, does a query, and changes a value in the database. Once I had that working I might think of doing the same thing from a GUI.
Reply
#3
by the way i solved the above problem. now it works flawlessly. but I couldn't find where to mark as solved.
Reply
#4
(Jul-30-2021, 06:57 PM)deanhystad Wrote: Have you tried writing a program that connects to the database, does a query and prints the result? Not a GUI program, just a console program. I would start with that. Once I had that working I would write another console program that connects to the data base, does a query, and changes a value in the database. Once I had that working I might think of doing the same thing from a GUI.

I do the same operations in this, but in the visual version. It would be helpful to make menu logic for the terminal.

1- View database tables

2- select the table x

2.1- inquire

2.2- add etc
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyQt5 form not displaying my data from SQLite3 Database Linuxdesire 2 4,983 Jan-10-2023, 09:51 PM
Last Post: gradlon93
  [PyQt] How can I sync Combobox index to other combobox index? nickzsche 2 2,395 Jan-03-2022, 12:29 PM
Last Post: Axel_Erfurt
  [Tkinter] Displaying Data from a database and run a function when clicked? PythonNPC 1 2,070 Mar-11-2020, 08:16 PM
Last Post: Larz60+
  [Tkinter] Update a combobox LagratteCchouette 0 3,857 Jan-12-2020, 01:44 PM
Last Post: LagratteCchouette
  Update value selected in option menu when select an item from text box klllmmm 2 5,023 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  3D data mapper with data labels using python modules lemon 0 2,034 Nov-15-2018, 09:17 AM
Last Post: lemon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020