PDA

View Full Version : SQL commande with QT



Ilumeo
8th January 2018, 00:03
Hello there,

I'm trying to search a special value into my database but it just doesn't want to get there.
I'm using QSQLITE to connect to my database and i'm using the following code to reach a value :




void DbManager::printCommande(int id)
{
QSqlQuery query("SELECT * FROM commandes WHERE id=(:id)");
query.bindValue(":id", id);
int idId = query.record().indexOf("id");
int idmagasin = query.record().indexOf("idMagasin");
int idclient = query.record().indexOf("idClient");
int idDate = query.record().indexOf("date_commande");
int idEmploye = query.record().indexOf("EmployeResponsableVente");
int idPaiement = query.record().indexOf("mode_paiement");
while (query.next())
{
QString name = query.value(idId).toString();
qDebug() << name;
QString magasin = query.value(idmagasin).toString();
qDebug() << magasin;
QString client = query.value(idclient).toString();
qDebug() << client;
QString date = query.value(idDate).toString();
qDebug() << date;
QString employe = query.value(idEmploye).toString();
qDebug() << employe;
QString paiement = query.value(idPaiement).toString();
qDebug() << paiement;

}
}


I've looked for a bit for a soluton but everywhere i've looked for the previous code worked for everybody but not me so i'm quite in a bind here.
I checked if it would work if I changed the (:id) into an int and it worked so I guess it's how I call the id that causes problems but i quite don't get it.

thanks in advance for your help

Lesiok
8th January 2018, 07:37
Where is query.exec() ?

Ilumeo
8th January 2018, 12:40
I didn't use any query.exec() untill now, that didn't seem necessary. Where should it be placed ?

Lesiok
8th January 2018, 12:51
1. Read more about QSqlQuery constructors.
2. It should be like this :
QSqlQuery query;
query.prepare("SELECT * FROM commandes WHERE id=(:id)");
query.bindValue(":id", id);
query.exec();

Ilumeo
8th January 2018, 13:04
Oh ok nice, from all the website I have looked over none talked about the prepare method, I guess my searching skill is not quite proefficient yet. Anyway Thank you that was really helpfull