1

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.

  • 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.
  1. The tools I am using:

VS c++ 2017, CMake, Qt 5.09.2, SQL server 2017

  1. 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")

  1. Test coding:

    This coding generate the error message above.

    int main()
    {
         QSqlDatabase GUIInpDB = QSqlDatabase::addDatabase("QODBC", "MainSQLDB");
         GUIInpDB.setConnectOptions();
         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
         GUIInpDB.setDatabaseName(connString);
         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();
    
         getchar();
         return 0;
    }
    
0

2 Answers 2

3

From ODBC function sequence error:

The documentation for the QSqlQuery constructor says "If query is not an empty string, it will be executed.", which means that when you call exec() you are executing it twice.

QSqlQuery::QSqlQuery(const QString &query = QString(), const QSqlDatabase &db = QSqlDatabase()).

Sign up to request clarification or add additional context in comments.

2 Comments

@sanwood thank you and this worked! I thought I already avoided doing the query twice but I obviously didn't!
Just to sum up and extract the solution from QtCentre link: The problem is that the query was executed twice: once in the QSqlQuery ctor and then, second time, in the exec() method. The solution is either to call the constructor with no query string (QSqlQuery query(db);) and then put the query string as exec()'s argument OR remove the query.exec() call, leaving the query string in the constructor.
1

I experienced the same error. However, I could not reconcile the reason for this being that the query was being executed twice. I eventually determined that my insert was referencing a column that did not exist in the table. Creating the table definition with the missing column fixed the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.