Load MySQL Database, can't connect to host. [SOLVED]



  • Hi,

    I am trying to connect to a database using the following code:
    @#include <QtCore/QCoreApplication>
    #include <QtSql>
    #include <iostream>
    #define q2c(string) string.toStdString()
    int main(int countArg, char **listArg)
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("pivoine");
    db.setUserName("root");
    db.setPassword("passwd");
    db.setDatabaseName("DBChemAlive");
    if(db.open())
    {
    std::cout << "Connected " << q2c(db.hostName()) << std::endl;
    db.close();
    }
    else
    {
    std::cout << "Error :(" << std::endl << q2c(db.lastError().text()) << std::endl;
    }}@

    Here the output at execution:
    Error :(
    Can't connect to MySQL server on 'pivoine' (111) QMYSQL: Unable to connect

    I don't know what to try, thanks for your help.


  • Moderators

    what about the port? Is the server running on a different port (default port for MYSQL server is 3306)? If so you need to specifiy it. But it wouldn't be bad to add it anyway to rid out this cause.



  • Can the application load the database plugin?
    Try to list the available database drivers with QSqlDatabase::drivers



  • In fact it was a stupid mistake of mane related to the hostame for mysql.
    So now it is fine.

    Thanks


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.