Search code examples
pyqt5python-3.7qsqldatabase

erratic QSqlDatabase behavior


I open a QSqlDatabase and load the parameter with db.set[parameter Name], The connection -db.open() fail and when checking the value of db.parameter it returns an empty string. The same form used in other client programs works perfectly....sic

I'm loading the parameters from a config file. I tried to load them manually like db.setUserName('someuser') with the same result. Debugging, when checking db.userName() I got str ''. Naturally the db was not to open. It might be related to the environment when the database is open?

@pyqtSlot()                                                                                              
def connectionTest(self):                                                                                
    self.lblTestResult.setText("Connecting to MYSQL server........")                                     
    self.lblTestResult.setStyleSheet("QLabel{background-color: yellow; color: black}")                   
    testMessage = "Connection Failed"                                                                    
    self.con_string = self.read_db_config()                                                              
    try:                                                                                                 
        db = QSqlDatabase.addDatabase("QMYSQL")                                                          
        db.setHostName(self.con_string['host'])                                                          
        db.setUserName(self.con_string['user'])                                                          
        db.setDatabaseName(self.con_string['database'])                                                  
        db.setPassword(self.con_string['password'])                                                      
        ok = db.open()                                                                                   
        if ok:                                                                                           
            testMessage = "Connection Succeeded"                                                         
            self.state = True                                                                            
            self.lblTestResult.setStyleSheet("QLabel{background-color: green; color: white}")            
        else:                                                                                            
            self.lblTestResult.setStyleSheet("QLabel{background-color: red; color: white}")              
        self.lblTestResult.setText(testMessage)                                                          
    except Exception as err:                                                                             
        self.lblTestResult.setText(err)                                                                  
    finally:                                                                                                                                      

I expect the parameters loaded and the db open successfully. I've been dealing with this issue for some time now without finding any clue of whats going on. Side Note: I'm using Exception to overcame the nice PyQt5 exception handling of shutting down the program.


Solution

  • I was able to solve the problem following 'Benjamin T' advise on a similar issue related to "Driver available but not loaded". I copied the libmysql.dll from the python 3.7/lib/site-packages folder to the python 3.7 executable folder and was able to load the driver and connect to MySQL database.