[Solved]How to use M.S access in Qt?



  • I want use access database in my project, this is my code:
    @#include <QCoreApplication>
    #include <QtSql/QSqlDatabase>
    #include <QDebug>
    #include <QStringList>

    int main(int argc, char argv[])
    {
    QCoreApplication a(argc, argv);
    QSqlDatabase m_db = QSqlDatabase::addDatabase("QODBC");
    m_db.setDatabaseName("DRIVER={Microsoft Access Driver (
    .mdb)};FIL={MS Access};DBQ=c:\db.mdb;");
    bool ok = m_db.open();
    if(ok)
    qDebug()<<"ok";
    else
    qDebug()<<"not ok";
    return a.exec();
    }@
    When I run it it shows mt 'not ok' I think I use wrong connection because QODBC driver in available,
    another question is where 'db.mdb' file must be located? in debug folder or it must be attached to the project and how the connection string should change ?



  • It Works with this connection string:
    @ m_db.setDatabaseName("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=" + QCoreApplication::applicationDirPath() + "/db.mdb");
    @


Log in to reply
 

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