Results 1 to 5 of 5

Thread: double record insertion

  1. #1
    Join Date
    Aug 2006
    Location
    Some where in Argentina
    Posts
    23
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Unhappy double record insertion

    I've got the follow problem with this query:

    Qt Code:
    1. QSqlQuery *cola2 = new QSqlQuery( "INSERT INTO preguntas ( pregunta, dificultad, referencia, respuesta ) VALUES ( ?, ?, ?, ? )" );
    2. cola2->bindValue( 0, c->texto );
    3. cola2->bindValue( 1, c->dificultad );
    4. cola2->bindValue( 2, c->referencia );
    5. cola2->bindValue( 3, c->respuesta );
    6. if( !cola2->exec() )
    7. {
    8. cout<<"agregar_pregunta()::Error al ejecutar la cola de insercion "<<endl;
    9. cout<<"agregar_pregunta()::Descripcion del error: "<<cola2->lastError().text()<<endl;
    10. TElog->append( "No se pudo agregar la pregunta a la db" );
    11. }
    12. else
    13. {
    14. cout<<"agregar_pregunta()::Registro Agregado"<<endl;
    15. agregadas++;
    16. }
    17. delete(cola2);
    To copy to clipboard, switch view to plain text mode 

    this query is ona cycle for each record that must be inserted... but at doing a export of the table gets one empty record and a good record..
    chequed that this query is executed only once in each record insertion.
    Thanks in advance...

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: double record insertion

    Show us your full cycle
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: double record insertion

    It should be:
    Qt Code:
    1. QSqlQuery *cola2 = new QSqlQuery();
    2. cola2->prepare( "INSERT INTO preguntas ( pregunta, dificultad, referencia, respuesta ) VALUES ( ?, ?, ?, ? )" );
    3. cola2->bindValue( 0, c->texto );
    4. cola2->bindValue( 1, c->dificultad );
    5. ...
    To copy to clipboard, switch view to plain text mode 
    BTW. Why do you allocate that query on the heap, if you delete it immediately? Won't it be simplier to create it on the stack?

  4. #4
    Join Date
    Aug 2006
    Location
    Some where in Argentina
    Posts
    23
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: double record insertion

    Quote Originally Posted by jacek
    It should be:
    Qt Code:
    1. QSqlQuery *cola2 = new QSqlQuery();
    2. cola2->prepare( "INSERT INTO preguntas ( pregunta, dificultad, referencia, respuesta ) VALUES ( ?, ?, ?, ? )" );
    3. cola2->bindValue( 0, c->texto );
    4. cola2->bindValue( 1, c->dificultad );
    5. ...
    To copy to clipboard, switch view to plain text mode 
    BTW. Why do you allocate that query on the heap, if you delete it immediately? Won't it be simplier to create it on the stack?
    thanks, the prepare function works fine now. About the deletion it`s necesary because Sqlite block the while db with each query, cursor, o whatever that uses the database.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: double record insertion

    Quote Originally Posted by tranfuga25s
    About the deletion it`s necesary because Sqlite block the while db with each query, cursor, o whatever that uses the database.
    But if you create that query object on the stack, it will be deleted too and additionally it will be faster and less error-prone.

Similar Threads

  1. MYSQL insert
    By allensr in forum Newbie
    Replies: 4
    Last Post: 14th August 2006, 16:55
  2. Push button double click
    By curtisw in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2006, 16:40
  3. Record update windowd entered data saving
    By MarkoSan in forum Qt Programming
    Replies: 56
    Last Post: 18th January 2006, 18:50
  4. Replies: 5
    Last Post: 12th January 2006, 15:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.