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. QSqlQuery adds records twice

QSqlQuery adds records twice

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqlquery
4 Posts 2 Posters 1.0k Views 2 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
    gabor53
    wrote on last edited by
    #1

    Hi,
    I have the following code:

     db = QSqlDatabase::addDatabase ("QSQLITE","submit");
       db.setDatabaseName (fileQstring );
       db.open ();
       QSqlQuery querys(db);
    
       if(!db.open ())
       {
           qDebug() << "The database is not open (submit)!";
       }
       else
       {
           qDebug() << "The database is open (submit)!";
       }
    
    	QFile file(fileName);
    
        if(file.open (QIODevice::ReadOnly))
        {
            qDebug() << "File " << fileName << " is open!";
            byteArray = file.readAll ();
        }
        else
        {
            qDebug() << "File " << fileName << " is NOT open!";
        }
    
        qDebug() << "Original data " <<byteArray.size() << "bytes.";
    
        qDebug() <<"Byte size: " << byteArray.size();
    
       querys.prepare("INSERT INTO Items (ID, Name, Pic, Description, Month, Day, Year, History, Age, Notes, Color, Material, Signed, What)" "VALUES(:ID, :Name, :Pic, :Description, :Month, :Day, :Year, :History, :Age, :Notes, :Color, :Material, :Signed, :What)" );
       querys.bindValue (":ID",sID);
       querys.bindValue (":Name",name);
       querys.bindValue (":Pic",byteArray);
       querys.bindValue (":Description",description);
       querys.bindValue (":Month",month);
       querys.bindValue (":Day",day);
       querys.bindValue (":Year",year);
       querys.bindValue (":History",history);
       querys.bindValue (":Age",age);
       querys.bindValue (":Notes",notes);
       querys.bindValue (":Color",newColor);
       querys.bindValue (":Material",newMaterial);
       querys.bindValue (":Signed",newSignedby);
       querys.bindValue (":What",newWhat);
    
       querys.exec ();
    
       if(!querys.exec())
       {
           qDebug() <<"Error inserting into the main db!" << querys.lastError ();
    
       }
    

    It works fine, but please help me to figure out why it inserts each record twice.
    Thank you.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @gabor53 said:

      querys.exec (); << first time

      if(!querys.exec()) << next time
      {

      Hi, you exceute it twice :)

      bool result = querys.exec ();
      if(!result)
      {

      G 1 Reply Last reply
      3
      • mrjjM mrjj

        @gabor53 said:

        querys.exec (); << first time

        if(!querys.exec()) << next time
        {

        Hi, you exceute it twice :)

        bool result = querys.exec ();
        if(!result)
        {

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        @mrjj
        I changed the last few lines like this:

           bool result = querys.exec ();
        
           if(!result)
           {
               qDebug() <<"Error inserting into the main db!" << querys.lastError ();
        
           }
        

        Now it adds a blank line, all fields with NULL in them. What did I mess up?
        Thanks.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          dont know. should work as before. just not inserting twice.

          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