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

"Driver not loaded" for using ODBC to read excel



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



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



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



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

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

    Does that change anything?



  • @Bonnie

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


Log in to reply