Reusing a QsqlQuery object too many times causes an unhandled exception SOLVED



  • I have a very simple Qt based application (4.6.3) running on a Window7 system that is connecting to an MSSQL database running on a separate server. I can successfully make a connection to the DB and perform queries successfully.
    However, if i attempt to reuse the same QSqlQuery object a number of time, the query.exec() generates an Unhandled exception. In the code snippet below, the 3rd query fails. I have simplified the code sample for this post. I have tried to do a query.finish() after each query has been processed with no success. I then tried to use query.clear(), but that would also ALWAYS throw an unhandled exception. no matter when it is executed.
    The code was built and tested via Visual Studio.

    If I use a new QSqlQuery option for each query, the queries succeed.
    Am I doing something incorrectly?
    Thank You,

    Arnie

    @
    int main( int argc, char *argv[])
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setHostName("myhostname");
    db.setDatabaseName("mydsn");
    db.setUserName("myuserid");
    db.setPassword("mypassword");
    bool ok = db.open();
    QSqlQuery query;

    query.exec("Select id from myTable");
    while (query.next())
    {
    int id = query.value(0).toInt();
    qDebug() << id;
    }

    query.exec("Select id from myTable");
    while (query.next())
    {
    int id = query.value(0).toInt();
    qDebug() << id;
    }

    query.exec("Select id from myTable"); //<--THIS query.exec never gets executed. The exception happens
    //here
    while (query.next())
    {
    int id = query.value(0).toInt();
    qDebug() << id;
    }
    ......
    @



  • My previous post should have said that the query.clear() ALWAYS throws an exception whenever it is executed. Sorry about that mistake.



  • I have solved this problem. It turns out that while I was linking in the wrong versions of the Qt dlls. I was in some cases linking in the the debug version in the release build or the release version in the debug build. Once I figured this all of the unhandled exceptions were resolved.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.