Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qsqldatabase can't connect to ms access database file



  • hi
    i have a ms access file ...my file name is mydb.mdb and i use win7 64x and Qt 5.1 msvc 2012.
    now i want to load my db file and access to it's saved information.i put my db file in my hard disk C drive.
    my code is:

    @#pro file
    QT +=sql
    @

    MainWindow.cpp
    @
    QSqlDatabase mydb = QSqlDatabase::addDatabase("QODBC","mybasedb");
    qDebug()<<mydb.isValid()<<endl;//true
    mydb.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=C:\mydb.mdb");
    mydb.setConnectOptions("SQL_ATTR_ODBC_VERSION=SQL_OV_ODBC3");//force ODBC3.x behavior
    mydb.setHostName("localhost");
    mydb.setDatabaseName("mydb");
    mydb.setUserName("");
    mydb.setPassword("");
    bool opened=mydb.open();
    if(opened)
    QMessageBox::information(this,"Yeah","It's Perfect!");
    else
    {
    QMessageBox::information(this,"Oh",mydb.lastError().text());
    qDebug()<<mydb.lastError().text()<<endl;
    }
    @
    now i am trying to connect and open my db file but it can't open db file.
    how can i solve it???

    the error message is:

    bq. [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect;

    also can i load db file as bytearray or file object and then use it by QSqlDatabase?


Log in to reply