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 Transactions and QSqlQuery Creation
Forum Updated to NodeBB v4.3 + New Features

QSqlDatabase Transactions and QSqlQuery Creation

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 10.4k 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.
  • J Offline
    J Offline
    joeuser
    wrote on last edited by
    #1

    The docs say "When using transactions, you must start the transaction before you create your query". But it doesn't say why nor what happens if you don't. Does anyone know why this is or the consequences of not doing it? It seems like this restriction really limits the usefulness of prepared queries.

    I've run a couple of test queries created before the transaction and everything seems OK. I can commit and rollback the transactions with no problems.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      broadpeak
      wrote on last edited by
      #2

      This is a perfect example:
      @
      QSqlDatabase::database().transaction();
      QSqlQuery query;
      query.exec("SELECT id FROM artist WHERE name = 'Gluecifer'");
      if (query.next()) {
      int artistId = query.value(0).toInt();
      query.exec("INSERT INTO cd (id, artistid, title, year) "
      "VALUES (201, " + QString::number(artistId)
      + ", 'Riding the Tiger', 1997)");
      }
      QSqlDatabase::database().commit();
      @

      Here an ID is queryied, and in the next query this ID is used. So, this ID won't be modified under the whole transactions! And this is an important thing.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        joeuser
        wrote on last edited by
        #3

        broadpeak, sorry but I don't see the relevance of your example to my question.

        What would be the consequences creating the QSqlQuery before starting the transaction:

        @
        QSqlQuery query;
        QSqlDatabase::database().transaction();

        query.exec("SELECT id FROM artist WHERE name = 'Gluecifer'");
        ...
        @

        In my testing there doesn't seem to be a difference but the docs say don't do that. I'm wondering why.

        What I want to do is implement a class that uses prepared queries to interact with the database. Like the following:

        @
        class MyData
        {
        public:
        MyData();
        ~MyData();

        void addCD(const QString& artistName, const QString& title, int year);
        

        private:
        QSqlQuery insCdQuery;
        QSqlQuery insArtistQuery;

        };

        MyData::MyData() : insCdQuery(), insArtistQuery()
        {
        insCdQuery.prepare( "INSERT INTO cd (artistid, title, year) VALUES (:artistid, :title, :year)" );
        insArtistQuery.prepare( "INSERT INTO artist (name) VALUES (:name)" );

        }

        void MyData::addCD(const QString& artistName, const QString& title, int year)
        {
        // assuming the artist doesn't exist yet, insert a new artist

        QSqlDatabase::database().transaction();
        
        insArtistQuery.bindValue( ":name", artistName );
        
        if ( insArtistQuery.exec() )
        {
            // get new artist ID
            int artistID = insArtistQuery.lastInsertId().toInt();
        
            insCdQuery.bindValue( ":artistid", artistID );
            insCdQuery.bindValue( ":title", title );
            insCdQuery.bindValue( ":year", year );
        
            if ( insCdQuery.exec() )
            {
                int cdID = insCdQuery.lastInsertId().toInt();
                
                // everything OK, commit transaction and return from method
                QSqlDatabase::database().commit();
                return;
            } 
            
        
        }
        
        // if we made it here something didn't go right, rollback the transaction
        QSqlDatabase::database().rollback();    
        

        }

        @

        But the docs suggest I shouldn't do this but doesn't give a reason.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          The query may not be executed in the transaction, I would suppose, probably depending on the driver.

          http://www.catb.org/~esr/faqs/smart-questions.html

          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