Please nominate your Qt Champions for 2021!

Connecting to MS SQL Server 2012 from Mac OS X Mavericks

  • Hi Everyone,

    I have searched the forum for an answer to this question but I can't seem to find one. I am trying to connect to a Microsoft SQL Database 2012 server fro Mac OS X Mavericks 10.9.4 as shown below but I don't know what to substitute for "DRIVER={SQL Native Client}":

    #include <QCoreApplication>
    #include <QtSql>
    #include <QDebug>

    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    QString serverName = "\\MSSQLSERVER";
    QString dbName = "AdventureWorks2012";
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    QString dsn = QString("DRIVER={SQL Native Client}; SERVER=%1; DATABASE=%2; UID=SA; PWD=******;").arg(serverName).arg(dbName);
        qDebug() << "Connected";
        qDebug() << "Errors = " << db.lastError();
    return a.exec&#40;&#41;;



  • Hi, it's possible for sure, but not as straightforward as on Windows. While you'll use the same Qt classes, like QSqlDatabase etc. you need to install ODBC support on your Mavericks system. I did this earlier this year and wrote a "DB library module ": with Qt that works both in Windows and Mac.

    On Windows you just can call setDatabaseName() and open(), as in your example above, but on Mavericks you'll first need to create 2 conf files: "/Users/username/Library/ODBC/odbc.ini" and "/Users/username/.freetds.conf" (as you see in my Github code).

    So what you need to install: /usr/lib/libiodbc.2.dylib (this is what Qt's DLL libqsqlodbc.dylib wants, if you do otool -L on it) and /usr/local/lib/ (as requested by the odbc.ini file you create).

    The first one is usually default installed even on Mavericks I think... Anyway you got some leads here :-)

Log in to reply