QSqlQuery causing ODBC Function sequence error

  • I searched this forum, SO, and Google but didn't find much help on this question. It seems due to ODBC functions being called out out of order. But since I am using QSql that wraps ODBC, it is hard for me to track down the function. Please help...

    • I was able to connect to the sql server database

    • I tested a very simply query and still got the error. I don't think it's due to column binding.

    • I was able to run the query with sql server, so I think the sql query is ok.

    The tools I am using:
    VS c++ 2017, CMake, Qt 5.09.2, sql server 2017
    Below are the error messages:

    QODBCResult::exec: Unable to execute statement: "[Microsoft][ODBC Driver Manager] Function sequence error" QSqlError("0", "QODBC3: Unable to execute statement", "[Microsoft][ODBC Driver Manager] Function sequence error")

    Test coding:
    This coding generate the error message above.

    int main()
        QSqlDatabase GUIInpDB = QSqlDatabase::addDatabase("QODBC", "MainSQLDB");
        QString inpSqlServer = "DESKTOP-085AEA8\\SQLEXPRESS";
        QString dbName = "test";
        QString connString = QString("Driver={ODBC Driver 13 for SQL Server};Server=%1;DATABASE=%2;Trusted_Connection=Yes;")
            .arg(inpSqlServer).arg(dbName); //the argument replace the %1 and %2
        QSqlDatabase db = QSqlDatabase::database("MainSQLDB");
        if (!db.open())
            qDebug() << "Connection for db not working";
            return 1;
        QSqlQuery query("SELECT * FROM TBL.tbl_test", db);  
        if (!query.exec())
            qDebug() << query.lastError();
        int num_of_rows = query.size();
        return 0;

  • Lifetime Qt Champion

    Hi, this looks like https://bugreports.qt.io/browse/QTBUG-54880 - can you please elaborate?

    btw: The ODBC driver does not support QSqlQuery::size() - see http://doc.qt.io/qt-5/qsqldriver.html#DriverFeature-enum

  • @Christian-Ehrlicher Thanks for your quick response. Yes I also noticed the bug report, and tried to apply the query using QSqlQuery::exec(), but still got the same problem. I also tried adding "QCoreApplication::processEvents();" before QSqlQuery. That does not solve the problem either. I have tried everything I can. It does look like a bug but I don't have enough experience to tell whether it's a bug. Can you please help me. Thanks!

  • @pdsc_dy
    That bug report is against SQL 2005, which is a lot older than 2017. Just saying.

    So they want you to try:

        QSqlQuery query(db);  
        if (!query.exec("SELECT * FROM TBL.tbl_test") ...


    Also what's your TBL referring to?

  • @JonB I think the user asking the question tried the QSqlQuery query(db) and it worked for him, but not for me.

    TBL is a schema I created in the sql database, using the following coding. Do you think this would cause issues?


  • Hi, if you're having doubts about your own created database, you could try using Qt for opening one of Microsoft's demo databases, for example the Northwind database

Log in to reply