connect to MySQL data base with QSqlDatabase pyqt4

529 views Asked by At

im new to python code, QT and MySQL, i wanna show a data base in a QtableView but i cant it. i dont know whats happend, the error is ('QSqlDatabase' object has no attribute 'setHotsName')

the code is:

# -*- coding: utf-8 -*-
import sys
import mysql.connector
from PyQt4 import QtSql, QtCore, QtGui, uic
form_class = uic.loadUiType("dialog.ui")[0]

def crearConexion():
  db = QtSql.QSqlDatabase.addDatabase('QMYSQL')
  db.setHotsName('localhots')
  db.setDatabaseName('Registro')
  db.setUserName('root')
  db.setPassword('xxxx')
  db.open()
  print(db.lastError().text())
  return True

 class MiClase(QtGui.QDialog):
  def __init__(self, parent=None):
   QtGui.QWidget.__init__(self, parent)
   self.setupUi(self)
   self.model=QtSql.QSqlTableModel(self)
   self.model.setTable("RegistroIN")
   self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
   self.model.select()
   self.tableView.setModel(self.model)
  if __name__ == '__main__':
   app = QtGui.QApplication(sys.argv)
   if not crearConexion():
     sys.exit(1)
 MiApp = MiClase()
 MiApp.show()
 sys.exit(app.exec_())

somebody that help me? thank you.

0

There are 0 answers