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:
    http://qt-project.org/doc/qt-4.8/sql-connecting.html
    And
    http://www.youtube.com/watch?v=3XE2bKUAxfw&list=SP2D1942A4688E9D63
    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");
    db.setHostName(servername);
    db.setDatabaseName(dbname);
    db.setUserName("root");
    db.setPassword("");

    if(db.open())
    qDebug()<<"Opened";

    else
    {
    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": http://qt-project.org/doc/qt-5.0/qtsql/sql-driver.html#qodbc 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
    http://www.youtube.com/watch?v=3XE2bKUAxfw

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

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

    does it look right?


Log in to reply
 

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