Strange behaviour of QSqlQuery
-
Hi everybody !
I have a problem with one of my queries, but not the others.
The query works when I write it "directly" on my server manager, but not via my Qt program.Here, in this code, the two last queries (insertPeseeDouble and updatePeseeOut) work when I call them in the program. But the first one (insertPeseeIn), when I call it in my program, fails. But works on the server manager.
All the three queries are used on the same page, by the same database and written on the same cpp file.
Here is the error I get :
@QSqlQuery::exec: database not open
"Driver not loaded Driver not loaded@@int AccesBD::insertPeseeIn(int idFkBalanceIn, int idFkTransport, int idFkProduit, float quantite, int IdFkUniteMesure, QString etat, bool utilisation)
{
QSqlQuery query;
if(query.exec(QString("INSERT INTO [dbo].[Pesee] (Datetime_in, Id_fk_balance_in, Id_fk_transport, Id_fk_produit, Quantite_in, Id_fk_unite_mesure, Etat, Utilisation) VALUES(CURRENT_TIMESTAMP, %1,%2,%3,%4,%5,'%6','%7');").arg(idFkBalanceIn).arg(idFkTransport).arg(idFkProduit).arg(quantite).arg(IdFkUniteMesure).arg(etat).arg(utilisation)))
{
QSqlQuery queryLast("SELECT IDENT_CURRENT('[dbo].[Pesee]');");
queryLast.next();
return queryLast.value(0).toInt();
}
return -1;
}int AccesBD::insertPeseeDouble(int idFkBalanceIn, int idFkBalanceOut, int idFkTransport, int idFkProduit, float quantiteIn, float quantiteOut, int IdFkUniteMesure, QString etat, bool utilisation)
{
QSqlQuery query;
if(query.exec(QString("INSERT INTO [dbo].[Pesee] (Datetime_in, Datetime_out, Id_fk_balance_in, Id_fk_balance_out, Id_fk_transport, Id_fk_produit, Quantite_in, Quantite_out, Id_fk_unite_mesure, Etat, Utilisation) VALUES(CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,%1,%2,%3,%4,%5,%6,%7,'%8','%9');").arg(idFkBalanceIn).arg(idFkBalanceOut).arg(idFkTransport).arg(idFkProduit).arg(quantiteIn).arg(quantiteOut).arg(IdFkUniteMesure).arg(etat).arg(utilisation)))
{
QSqlQuery queryLast("SELECT IDENT_CURRENT('[dbo].[Pesee]');");
queryLast.next();
return queryLast.value(0).toInt();
}
return -1;
}bool AccesBD::updatePeseeOut(int idPesee,int idFkBalanceOut, float quantiteOut)
{
QSqlQuery query;
return query.exec(QString("UPDATE [dbo].[Pesee] SET Datetime_out = CURRENT_TIMESTAMP, Id_fk_balance_out = %1, Quantite_out = %2 WHERE Id_Pesee = %3").arg(idFkBalanceOut).arg(quantiteOut).arg(idPesee));
}@I really dont understand why :(
If someone get an idea about that, it would be great :D
Thanks anyway for the attentions. -
Hi, what is your query.lastError?
-
It is :
"Driver not loaded Driver not loaded"
-
As the other queries work, this messages seems to be misleading.
I suggest to look at the QString for your query by using qDebug().
Probably this may be useful for you:
@
QString s = "INSERT INTO [dbo].[Pesee] ...";
query.prepare(s);
if (!query.exec()) {
QMessageBox::warning(this, "ERR", "Error in query!\n" + query.lastError().text(), QMessageBox::Ok);
qDebug() << s;@
-
That's not that finally -_-
I test the call of this query in a basic program and it work.
Actually, I just saw that in my original program, two of the arguments are null (= 0) and can't work because of the relation with other table I guess...
I'll fix it before anything else and see what happen after that.Thanks anyway ^^
-
You're welcome.
Have you considered to work with QSqlQuery::bindValue? This method is more transparent and easier to debug. -
I tried to use this at the beginning, but I got some problems. I followed some tutorial about that but nothing worked so I changed my queries in that way :/
-
I use it like this:
@
query = QSqlQuery(db);
QString s = "UPDATE mytable SET\n"
"field1 = :p01, field2 = :p02\n"
"WHERE fieldidx = :pID;";
query.prepare(s);
// WHERE clause:
query.bindValue(":pID", ui->lineEditID->text());
// SET fields:
query.bindValue(":p01", ui->lineEdit01->text());
query.bindValue(":p02", ui->lineEdit02->text());if (!query.exec()) {
// show lastError:
qDebug() << query.lastError();
// show QString of query:
qDebug() << s;
}
@ -
Is there a difference of performance/speed between the two methods ?
-
I did not test it up to now.
But I am working with very large sql queries and I am absolutely satisfied with the performance (with both methods!). -
I'll try again the bindValue so ^^
thx for your replies ;-)