Can't make query for Microsoft acess database using Qt



  • I try to connect to an access database and it connects fine but when I try to make a query I got error message here is the code I use to connect and make the query

    void MainWindow::connectToDatabse()
    {
        dp = QSqlDatabase::addDatabase("QODBC");
        dp.setDatabaseName("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=D:/b.accdb");
    
        if(dp.open())
          qDebug() << "oK";
        else
          qDebug() << dp.lastError().text();
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        QSqlQuery q("SELECT * FROM User",dp);
        if (!q.exec()){
            qDebug() << "error = " << q.lastError().text();
        }
        else {
            while (q.next())
            {
                qDebug() << q.value(0).toString();
            }
        }
    
    }
    

    when I use the connect to database function it connect and give me "OK" but when I use the button to make the query and get the data I got this error

    QODBCResult::exec: Unable to execute statement: "[Microsoft][ODBC Driver Manager] Function sequence error"
    error =  "[Microsoft][ODBC Driver Manager] Function sequence error QODBC3: Unable to execute statement"
    

    I go to Microsoft website to check the query statement if it's syntax is correct or wrong and I found it correct from this website here
    so what makes this error and not select the data from the database
    Thanks in advance



  • @AmrCoder

    Try to use an semi-colon at the command line end:

        QSqlQuery q("SELECT * FROM User;", dp);
    

    The semi-colon is shown at the line end of command on the page you are referring to.

    IIRC SQL is not really happy about sloppy syntax handling.



  • same problem



  • @AmrCoder Hi, when you construct the QSqlQuery object (q) MS Access does the query for you. Then after that you do another query (exec with an empty string). So the reason I think for the error is because you forgot a prepare() before the exec() call.

    But easiest I think is not to use exec() at all, you can try something like:

    QSqlQuery q("SELECT * FROM User",dp);
    while (q.next())
    {
        qDebug() << q.value(0).toString();
    }

Log in to reply
 

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