title: How to show the contents of a sqlite table in a QTableView and update the DB and have the results show in QTableView?
body: <p>I want to update a table/insert new item with an execute statement in qt and I want the table to immediately show this new data. What is the simplest possible example to do this?</p>
  
  <p>I am using python, sqllite and pyqt5. I have been looking for how to do this but without success.</p>
  
  <p>I am assuming it won't hog the UI or maybe you have to use threading then. In that case, I want to use QThread.</p>
  
  <p>edit: ok i found an example which i converted to PyQt5, added an edit func and cleaned up. Now just need to figure out how to delete all entries at once and see if using a model with qthread and sqlite will be responsive:</p>
  
  <pre><code>import sys
  from PyQt5 import QtCore, QtWidgets, QtGui, QtSql
  
  def initializeModel(model):
      model.setTable('sportsmen')
      model.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)
      model.select()
      model.setHeaderData(0, QtCore.Qt.Horizontal, "ID")
      model.setHeaderData(1, QtCore.Qt.Horizontal, "First name")
      model.setHeaderData(2, QtCore.Qt.Horizontal, "Last name")
  
  def createView(title, model):
      view = QtWidgets.QTableView()
      view.setModel(model)
      view.setWindowTitle(title)
      return view
  
  def addrow():
      print(model.rowCount())
      ret = model.insertRows(model.rowCount(), 1)
      print(ret)
  
  def editrow():
      model.setData(model.index(1, 1), "LEL")
      #model.submitAll()
  
  def findrow(i):
      delrow = i.row()
  
  if __name__ == '__main__':
  
      app = QtWidgets.QApplication(sys.argv)
      db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
      db.setDatabaseName('sports.db')
      model = QtSql.QSqlTableModel()
      delrow = -1
      initializeModel(model)
  
      view1 = createView("Table Model (View 1)", model)
      view1.clicked.connect(findrow)
  
      dialog = QtWidgets.QDialog()
      layout = QtWidgets.QVBoxLayout()
      layout.addWidget(view1)
  
      addBtn = QtWidgets.QPushButton("Add a row")
      addBtn.clicked.connect(addrow)
      layout.addWidget(addBtn)
  
      editBtn = QtWidgets.QPushButton("edit a row")
      editBtn.clicked.connect(editrow)
      layout.addWidget(editBtn)
  
      delBtn = QtWidgets.QPushButton("del a row")
      delBtn.clicked.connect(lambda: 
      model.removeRow(view1.currentIndex().row()))
      layout.addWidget(delBtn)
      dialog.setLayout(layout)
      dialog.setWindowTitle("Database Demo")
      dialog.show()
      sys.exit(app.exec_())
  </code></pre>