Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt5 MYSQL “open()” method return true even with wrong credentials



  • While writing a Qt5 application that uses QSqlDatabase with MySQL, I encountered a strange behavior when I try to connect to datebase. QSqlDatabase::open() returns true even with wrong username, password and port. That's my code:

    QSqlDatabase DB = QSqlDatabase::addDatabase("QMYSQL", "Connection");
    DB.setHostName(Interface.InputIP->text());
    DB.setDatabaseName("connectdb");
    DB.setPort(Interface.InputPort->value());
    DB.setUserName(Interface.InputLog->text());
    DB.setPassword(Interface.InputPass->text());
    bool result = DB.open();
    if(result)
    	QMessageBox::information(0, "Success", "Connected");
    else
    	QMessageBox::information(0, "Error", DB.lastError().text());
    DB.close();
    

    What can be the reason?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Just a silly idea but, are you sure the database you are accessing is protected ?



  • I'll write differently.
    If i write all credentials correctly i can connect to database, and method "open" return true which is proper behavior.
    But, if i write all credentials incorrectly (without IP) or i try to connect to disabled database method "open" again return true.
    If I use external application to connect to my database and it's works perfectly.


  • Lifetime Qt Champion

    Did you close the connection first ?



  • No, I close connection after try to connect and after conditional statement, as it is in the code in first post.


Log in to reply