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}":

    [code]
    #include <QCoreApplication>
    #include <QtSql>
    #include <QDebug>

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

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

    }

    [/code]



  • 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 ":https://github.com/Clinware/CWDB/blob/master/CWDB.cpp 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/libtdsodbc.0.so (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
 

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