QSqldatabase using transaction



  • I am writing an application that writes some results to the SQLite database. In the function here it is called for each of the 10 record. The challenge is the very first call to transaction() fails, but works for transactions 2-10. When I call it again no matter how many times I get all 10 records. Can anyone see what I am doing wrong? It is just the first call that fails which makes me think it is an order issue. Any help is appreciated.

    @
    bool cSOHDataPackage::writeSOHToDatabase(int inRecord)
    {
    //Open DB Connection
    //Iterate through the numOfMissionCatalogs
    bool success = false;

    if(!QSqlDatabase::contains("SOH"))
    {
        commandDB = QSqlDatabase::addDatabase("QSQLITE", "SOH");
        commandDB.setDatabaseName("C:/Sqltest/commandDB.sqlite");
    
    }
    else
    {
        commandDB = QSqlDatabase::database("SOH");
    
    }
    
    success = commandDB.transaction();
    if(success == false)
    {
        const QSqlError error = commandDB.lastError();
        qDebug() << "Error is " << error.text();
        return success;
    
    }
    
    
    success = commandDB.open();
    if(success == false)
    {
        const QSqlError error = commandDB.lastError();
        qDebug() << "Error is " << error.text();
        return success;
    
    }
    
    if(success == true )
    {
        if(!commandDB.tables().contains("SOHData"))
        {
            qDebug() << "SOH Data Table does not exist";
        }
        else
        {
            QSqlQuery query(commandDB);
            query.prepare("INSERT INTO SOHData (LogDateTime)"
                                   "VALUES (:LogDateTime)");
    
             query.bindValue(":LogDateTime", QDateTime::currentDateTime().toString());
    
            bool queryWorked = query.exec&#40;&#41;;
            if(queryWorked == true&#41;
            {
                qDebug(&#41; << "Record " << inRecord << " Created";
                //return true;
            }
            else
            {
    
                const QSqlError error = query.lastError(&#41;;
                qDebug() << "Error from SqlQuery = " << error.text();
                return false;
            }
    
        }
    
        commandDB.close();
        return success;
    }
    else
    {
        commandDB.close();
        return false;
    }
    

    }

    @


Log in to reply
 

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