QSqlQuery causing ODBC Function sequence error

戏子无情 提交于 2019-12-25 01:22:44

问题


I searched 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.

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

  2. 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;
}

回答1:


I found this discussion on QtCenter that might help you, even if I am doubtful about why it fixed the issue You migth try to use the QSqlQuery constructor that does not exec as mentionned

http://www.qtcentre.org/threads/18832-ODBC-function-sequence-error



来源:https://stackoverflow.com/questions/49472159/qsqlquery-causing-odbc-function-sequence-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!