python - PyQt5 QSqlTableModel not updating changes to database -


i have pyqt5 application connecting mysql database. made 2 views connected same model. views pull in data database fine. change make field in 1 reflected in other. not update database. tried leaving idle 15 minutes , didn't update database.

this code:

from pyqt5.qtsql import qsqldatabase, qsqlquery, qsqltablemodel pyqt5.qtwidgets import qtableview, qapplication import sys  app = qapplication(sys.argv)  db = qsqldatabase.adddatabase("qmysql") db.sethostname("localhost") db.setdatabasename("usrdb") db.setusername("usr") db.setpassword("passwrd") db.open()  testmodel = qsqltablemodel() qry = qsqlquery("select * test", db) testmodel.setquery(qry) testmodel.seteditstrategy(qsqltablemodel.onfieldchange)  testview = qtableview() testtview.setmodel(testmodel) testview2 = qtableview() testview2.setmodel(testmodel)  testview.show() testview2.show()  app.exec_() 

the second problem i'm having is, once change data in 1 cell, view doesn't let me edit data in other cell. selectable, not editable.

fixed both problems. pyqt documentation advices not use setquery.

you should not call it(setquery) on qsqltablemodel. instead, use settable(), setsort(), setfilter(), etc., set query.

updated code:

from pyqt5.qtsql import qsqldatabase, qsqlquery, qsqltablemodel pyqt5.qtwidgets import qtableview, qapplication import sys  app = qapplication(sys.argv)  db = qsqldatabase.adddatabase("qmysql") db.sethostname("localhost") db.setdatabasename("usrdb") db.setusername("usr") db.setpassword("passwrd") db.open()  testmodel = qsqltablemodel() testmodel.settable("test") testmodel.seteditstrategy(qsqltablemodel.onfieldchange) testmodel.select()  testview = qtableview() testtview.setmodel(testmodel)  app.exec_() 

this updates database edits made, , lets me multiple edits on view.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -