Python Forum

Full Version: How to update selected data in combobox displaying DB data?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
by the way i solved the above problem. now it works flawlessly. but I couldn't find where to mark as solved.
(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