Unable to display info from MySQL in PyQT app using QTableView

1.1k views Asked by At

I am busy with a university project where I need to create an SQL db and be able to work with the data through a program created in Python and PyQT.

I have created the database, added tables and some data in the tables, but now I need to use the QTableView widget to display the data and when I run it I just get a blank window.

I have noticed that if I change details such as username, password or database name to something random, I still get the same result which tells me that the connection to the database doesn't happen.

Can someone please look at my code and assist? Even with just some advice to get an error message?

from __future__ import division
import sys
from PyQt4 import QtCore, QtGui, QtSql

from showrec import*

def createConnection():
    db=QtSql.QSqlDatabase.addDatabase('QMYSQL')
    db.setHostName('localhost')
    db.setDatabaseName('database')
    db.setUserName('username')
    db.setPassword('password')
    db.open()
    print(db.lastError().text())
    return True

class MyForm(QtGui.QDialog):
    def _init_(self,parent=None):
        QtGui.QWidget._init_(self,parent)
        self.ui=Ui_Dialog()
        self.ui.setp.Ui(self)
        self.model=QtSQL.QSqlTableModel(self)
        self.Model.SetTable("table1")
        self.Model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
        self.model.select()
        self.ui.tableView.SetModel(self.model)

if __name__ == "__main__":

    app=QtGui.QApplication(sys.argv)
    if not createConnection():
        sys.exit(1)
    myapp=MyForm()
    myapp.show()
    sys.exit(app.exec_())`
1

There are 1 answers

0
Leon On

OK. I have found my mistake I place only one _ before and after init instead of

def _init_(self,parent=None):

instead of:

def __init__(self,parent=None):

silly mistake but one that had me busy for hours. Thanks for the response JonBrave