0

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.

Erick
  • 301
  • 3
  • 12
  • Add the line `print(db.lastError().text())` after `ok = db.open()`. What is the output? Also, add the line `print(self.con_string)` and show the output of that as well. – ekhumoro Feb 10 '19 at 18:15
  • PS: the "nice PyQt5 exception handling of shutting down the program" is completely normal and expected behaviour. In any python program, an unhandled exception immediately stops execution and a traceback is printed to stdout/stderr. If you want different behaviour, use an [except-hook](https://docs.python.org/3/library/sys.html#sys.excepthook). – ekhumoro Feb 10 '19 at 18:22
  • db.lastError().text() produces an expected "Driver not loaded" which it's reasonable since all db parameters are the empty string. Regarding con_string it a nice dictionary like{'host':'hostName', 'database'::'databaseName, 'user': 'userName', 'password': 'password'} fully loaded a expected. I also tryied to supply the parameters manually like db.setUser('str[userName]} with the same results. – Erick Feb 10 '19 at 23:33
  • What platform are you on? Are you running the code via some kind of IDE? Are you absolutely sure you've installed all the necessary qt packages? I doubt whether all the sql pulgins are always installed by default on all platforms. – ekhumoro Feb 11 '19 at 00:27
  • Good suggestion ekhumoro. It seems I have narrowed my problem to a "Driver present but not loaded" I didn't mentioned that the form works fine but on "other machine" windows 8 ok, windows 10 did not.. I'll try to work it out. – Erick Feb 11 '19 at 00:36
  • In regard to the PyQt5 error handling behavior it does not have the expected python behavior as ekkumoro mentioned, but it just crush the program without any trace back, so if you don,t use an Exception where the program crushes you don't know the cause of the crush. – Erick Feb 11 '19 at 00:40
  • It most definitely does have the expected behaviour. If you're not seeing a traceback, it's because you're probably using some buggy or badly configured IDE that doesn't know how to deal with exceptions properly. If you run the code in a normal console, you will see the python traceback and/or the qt error messages. – ekhumoro Feb 11 '19 at 00:46
  • To get diagnostic info about the plugins qt tries to load at runtime, run your code in a normal console like this: `QT_DEBUG_PLUGINS=1 python myscript.py` (or whatever the equivalent syntax is on your platform). – ekhumoro Feb 11 '19 at 00:47
  • Possible duplicate of [qt 5.8 sql connection error:QMYSQL driver not loaded on windows 10](https://stackoverflow.com/questions/42683438/qt-5-8-sql-connection-errorqmysql-driver-not-loaded-on-windows-10) – ekhumoro Feb 11 '19 at 22:04

1 Answers1

0

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.

Erick
  • 301
  • 3
  • 12