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

[solved] Unable to connect to MySql



  • I am trying connecting to mysql database on my computer. Here is what i tried:
    @void SQL_Interface::on_pushButton_clicked()
    {
    dataBase = QSqlDatabase::addDatabase("QMYSQL");
    dataBase.setHostName("root@localhost");
    dataBase.setDatabaseName("testBase");
    dataBase.setUserName("root");
    dataBase.setPassword("xubuntu");
    dataBase.setPort(3306);
    //dataBase.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");

    bool ok = dataBase.open();
    if(ok)
    {
        view = new QTableView;      // doing something to ensure that connection was successfull
        view->show();
    }
    

    }@

    Unfortunately, nothing happens. Here is what I do to start mysql from terminal:
    @sudo myql -p@



  • Did you try to get any errors by using "QSqlDatabase::lastError()":http://qt-project.org/doc/qt-5.0/qsqldatabase.html#lastError? This might provide some hints about what's wrong. Try something like this:
    @...
    bool ok = dataBase.open();
    if(ok)
    {
    ...
    }
    else
    {
    qDebug() << "Error while opening DB connection: " << dataBase.lastError().text() << endl;
    }@



  • First of all, take out the root@ from hostname, and make a little check on what happened at connection for example:
    @
    if(ok)
    {
    ...
    }else{
    qDebug() << "SQL Error :" << database.lastError().text();
    }
    @

    And check what is the error.



  • upz, double answer ^_^



  • Its working fine now; i commented two statements:
    @//dataBase.setHostName("root@localhost");
    // dataBase.setDatabaseName("testBase"); @

    there was no dB by the name testBase in mysql
    Thanks for your replies!



  • we are glad to help :)



  • Please mark topics that are resolved as solved, not as closed. Threads can be closed by forum admins only and means that the comments are disabled. Thanks.



  • I will be careful next time. thanks!



  • No problem - every forum handles these things different. Once we have the "QnA features":/forums/viewforum/46/ live on the general forums, marking a thread as solved will be much easier.


Log in to reply