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

QSQLDriver and Microsoft Access: Crash on insert statement?



  • Whenever I execute an insert statement from QtC++ I get an unhandled exception from the ODBC driver resulting in a crash. :frowning: Code is this:

    DBAccess::m_db = QSqlDatabase::addDatabase("QODBC");
           DBAccess::m_db.setHostName("localhost");
           DBAccess::m_db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=" + elLoc + "");//C:\\Dev\\AccessDB\\SVLearn_MNIST.accdb");
           DBAccess::m_db.setConnectOptions("SQL_ATTR_ODBC_VERSION=SQL_OV_ODBC3");
    
           bool  ok = DBAccess::m_db.open();
           qDebug() << m_db.driverName();
           if(ok)
               qDebug()<<"ok";
           else
               qDebug()<<"not ok"<< m_db.lastError().text();
    

    Which is successful (ok)

    query.prepare("INSERT INTO t_MNIST ( str64Data, blnTest, intLabel, intGroup ) VALUES ('Get a life', False, 0, 0);");
            if (!query.next()) {
                qDebug() << (query).lastError();
                LOG_FAILED_QUERY(query);
                return;
            }
    

    Which crashes with a hidden exception thrown.



  • @MGirgis
    Hi and welcome.

    You're supposed to go https://doc.qt.io/qt-5/qsqlquery.html#exec-1 :

    query.prepare();
    query.exec();
    query.next();
    


  • @JonB said in QSQLDriver and Microsoft Access: Crash on insert statement?:

    query.prepare();
    query.exec();
    query.next();

    Hello,

    I tried the above fix by inserting query.exec(); and it still throws an exception when it gets to that line.

    b7dafe42-ab93-471e-a09e-1e6e3f76330d-image.png



  • @MGirgis
    Show code --- we still don't know for example where you declared query, m_db looks like a member variable, yet DBAccess::m_db looks static.

    Better still, run this code under debugger. Look at the stack trace at the exception. I don't know which "that line" is among the lines you have.

    Having said that, I know nothing about QSQLDriver and Microsoft Access.


Log in to reply