Nominate our 2022 Qt Champions!

Database Connection on Mac

  • Hi Guys

    I am trying to connect to a database on my mac but looks nothing works. I am really newbie and following documentations, tutorial.

    I have setup MySQL workbench and XAMPP running on my mac but all I get is
    ERROR: "[iODBC][Driver Manager]Data source name not found and no default driver specified. Driver could not be loaded QODBC3: Unable to connect"

    I tried to follow this:
    Am I taking a totally wrong path?
    My Conde is as simple as:
    #include <QCoreApplication>
    #include <QtSql>
    #include <QtDebug>
    #include <QString>
    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);
    QString servername="localhost";
    QString dbname="test";
    QSqlDatabase db =QSqlDatabase::addDatabase("QODBC");


    qDebug()<<"ERROR: "<<db.lastError().text();
    return a.exec();

  • Well, the error message tells you that "test" is not a data source name (DSN) known to your ODBC install. From the "documentation": for the QODBC plugin:

    bq. Be aware that when connecting to an ODBC datasource you must pass in the name of the ODBC datasource to the QSqlDatabase::setDatabaseName() function rather than the actual database name.

    There is a native MySQL database driver plugin that you could (should) be using instead.

  • Thank you. To me seems there is something wrong with by DB setup. In JDBC using Eclipse, we need to download a JAR file then set the path in Eclipse. Does this apply in here too?

  • I tried to follow this tutorial

    I believe my connection string is somehow wrong. Here is what I found in SQL documentation for connection string

    @QString dsn = QString("Server=;Database=test;Uid=root;Pwd=");@

    does it look right?

Log in to reply