Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqldatabase using transaction
Forum Updated to NodeBB v4.3 + New Features

QSqldatabase using transaction

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 830 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gundi
    wrote on last edited by
    #1

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

    }

    @

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved