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. "Driver not loaded" for using ODBC to read excel
Forum Updated to NodeBB v4.3 + New Features

"Driver not loaded" for using ODBC to read excel

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 693 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.
  • H Offline
    H Offline
    hehedaozuiteng
    wrote on last edited by hehedaozuiteng
    #1

    Hi all, I am trying to use the QSqlDatabase to read an excel file, and followed the example from the https://wiki.qt.io/Handling_Microsoft_Excel_file_format

    here is my code:

    QString filename = QFileDialog::getOpenFileName(this,"open file",QDir::currentPath());
    qDebug()<<filename;
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "xlsx_connection");
    db.setDatabaseName("DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" +filename+"");
    
     if(db.open()){
    
      QSqlQuery query("select * from [" + QString("Sheet1") + "$]"); 
       qDebug() <<query.lastError();
      while (query.next())
       {
        QString column1= query.value(0).toString();
        qDebug() << column1;
        }
        db.close();
        QSqlDatabase::removeDatabase("xlsx_connection");
        }
        else{
        qDebug()<<db.lastError();
       }
    

    the result I get is :

    "E:/a.xlsx"
    QSqlQuery::exec: database not open
    QSqlError("", "Driver not loaded", "Driver not loaded")
    QSqlDatabasePrivate::removeDatabase: connection 'xlsx_connection' is still in use, all queries will cease to work.
    

    drivers list= ("QSQLITE", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
    I installed the both of Excel and engine(Microsoft Access Database Engine 2010).

    I checked my ODBC manager which is 64-bit, and my compilar is minGW-64 bit. there is exist a qsqlodbc.dll in sqldrive. folder.

    I solved it. Should called the QSqlQuery query("soem query",db); once I build a query, and I have to linked to database.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      Have you got Office installed?
      Or did you see the notice from the example?

      NOTE: To use ODBC on Windows, the MS Access Database Engine has to be installed. You can find it here: Microsoft Access Database Engine 2010. The Engine is maybe distributed with a MS Office Access installation, but on this should not be relied on. In Addition, you should regard that a 64 bit application can only use the 64 bit Engine and so for 32 bit accordingly. That’s why you maybe install both versions to avoid problems. Furthermore, the Engine should not be confused with the MS Access Runtime which contains the Engine.

      H 1 Reply Last reply
      1
      • B Bonnie

        Have you got Office installed?
        Or did you see the notice from the example?

        NOTE: To use ODBC on Windows, the MS Access Database Engine has to be installed. You can find it here: Microsoft Access Database Engine 2010. The Engine is maybe distributed with a MS Office Access installation, but on this should not be relied on. In Addition, you should regard that a 64 bit application can only use the 64 bit Engine and so for 32 bit accordingly. That’s why you maybe install both versions to avoid problems. Furthermore, the Engine should not be confused with the MS Access Runtime which contains the Engine.

        H Offline
        H Offline
        hehedaozuiteng
        wrote on last edited by
        #3

        @Bonnie

        I installed the both of Excel and engine(Microsoft Access Database Engine 2010).

        I used call the "db.open()" without engine, and it gives me a false. That is why I instell the engine. The odd thing is, I get a true with "db.open()", but it gives me a "Driver not loaded".

        B 1 Reply Last reply
        0
        • H hehedaozuiteng

          @Bonnie

          I installed the both of Excel and engine(Microsoft Access Database Engine 2010).

          I used call the "db.open()" without engine, and it gives me a false. That is why I instell the engine. The odd thing is, I get a true with "db.open()", but it gives me a "Driver not loaded".

          B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #4

          @hehedaozuiteng Seems the example is missing something.
          Try replace with below code

          QSqlQuery query("select * from [" + QString("Sheet1") + "$]", db); 
          

          Does that change anything?

          H 1 Reply Last reply
          1
          • B Bonnie

            @hehedaozuiteng Seems the example is missing something.
            Try replace with below code

            QSqlQuery query("select * from [" + QString("Sheet1") + "$]", db); 
            

            Does that change anything?

            H Offline
            H Offline
            hehedaozuiteng
            wrote on last edited by
            #5

            @Bonnie

            ye, I just figure out. Thanks for you help. should not coding when I lose a sleep.

            1 Reply Last reply
            0

            • Login

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