Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't make query for Microsoft acess database using Qt

Can't make query for Microsoft acess database using Qt

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmrCoder
    wrote on 3 Nov 2017, 22:32 last edited by
    #1

    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

    K 1 Reply Last reply 4 Nov 2017, 11:00
    0
    • A AmrCoder
      3 Nov 2017, 22:32

      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

      K Offline
      K Offline
      koahnig
      wrote on 4 Nov 2017, 11:00 last edited by kshegunov 11 Apr 2017, 13:45
      #2

      @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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AmrCoder
        wrote on 4 Nov 2017, 14:35 last edited by
        #3

        same problem

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hskoglund
          wrote on 4 Nov 2017, 15:12 last edited by
          #4

          @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();
          }
          1 Reply Last reply
          3

          1/4

          3 Nov 2017, 22:32

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved