I wrote something very similar for a project I did, where I basically had a database interface in the GUI, and I allowed the user to edit and add entries to the database without having to go into the MySql Workbench.
You're going to need two functions to interact with your database, one to retrieve the entries, and one to execute the changes.
This is some code that will retrieve information from the database using SqlAlchemy.
def get_info():
sql = text('SELECT id, '
'serial_number, '
'weight_name, '
'units, '
'nominal, '
'customer_name, '
'density, '
'density_uncert , '
'volumetric_exp, '
'comment '
'FROM weights_external '
'ORDER BY id ')
return self.engine.execute(sql).fetchall()
This selects the id, serial number, units, etc. from the table called "weights_external", ordering it by id. self.engine.execute(sql).fetchall()
returns a generator (I think, it might be a list but I don't remember off the top of my head sorry!) which you can loop through and get all the results. In this example, I'm getting 10 columns from this table. If the table had 100 rows, then the function would return a list with length 100, and each element of this list would be a list with length 10, each containing the information from one column.
I store this length-100 list in a variable, and I use this variable to populate a QTableWidget for the GUI.
I have a separate button to bring up a sub-window that allows the user to add a row. The code for this is very similar to get_info()
except that you are now updating or inserting into a table (you can look this up on SqlAlchemy or MySql documentation). Basically, you'll have something similar to this:
def push_data(self, data):
table = self.weights_external.insert()
self.engine.execute(table.values(id=data['id'],
serial_number=data['serial_num'],
...
comment=data['comment']))
I left out the stuff in the middle for sake of time and space, but these few snippets of code should allow you to retrieve data from the database, put it into a QTableWidget or another QObject, and allow the user to change that data with the push of a button. If you have any questions, please comment!