PDA

View Full Version : double record insertion



tranfuga25s
9th September 2006, 00:38
I've got the follow problem with this query:


QSqlQuery *cola2 = new QSqlQuery( "INSERT INTO preguntas ( pregunta, dificultad, referencia, respuesta ) VALUES ( ?, ?, ?, ? )" );
cola2->bindValue( 0, c->texto );
cola2->bindValue( 1, c->dificultad );
cola2->bindValue( 2, c->referencia );
cola2->bindValue( 3, c->respuesta );
if( !cola2->exec() )
{
cout<<"agregar_pregunta()::Error al ejecutar la cola de insercion "<<endl;
cout<<"agregar_pregunta()::Descripcion del error: "<<cola2->lastError().text()<<endl;
TElog->append( "No se pudo agregar la pregunta a la db" );
}
else
{
cout<<"agregar_pregunta()::Registro Agregado"<<endl;
agregadas++;
}
delete(cola2);

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...:)

zlatko
9th September 2006, 10:38
Show us your full cycle

jacek
9th September 2006, 13:40
It should be:
QSqlQuery *cola2 = new QSqlQuery();
cola2->prepare( "INSERT INTO preguntas ( pregunta, dificultad, referencia, respuesta ) VALUES ( ?, ?, ?, ? )" );
cola2->bindValue( 0, c->texto );
cola2->bindValue( 1, c->dificultad );
...

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?

tranfuga25s
9th September 2006, 16:08
It should be:
QSqlQuery *cola2 = new QSqlQuery();
cola2->prepare( "INSERT INTO preguntas ( pregunta, dificultad, referencia, respuesta ) VALUES ( ?, ?, ?, ? )" );
cola2->bindValue( 0, c->texto );
cola2->bindValue( 1, c->dificultad );
...

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.:rolleyes:

jacek
9th September 2006, 17:12
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.