Results 1 to 5 of 5

Thread: SQL commande with QT

  1. #1

    Default SQL commande with QT

    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 :

    Qt Code:
    1. void DbManager::printCommande(int id)
    2. {
    3. QSqlQuery query("SELECT * FROM commandes WHERE id=(:id)");
    4. query.bindValue(":id", id);
    5. int idId = query.record().indexOf("id");
    6. int idmagasin = query.record().indexOf("idMagasin");
    7. int idclient = query.record().indexOf("idClient");
    8. int idDate = query.record().indexOf("date_commande");
    9. int idEmploye = query.record().indexOf("EmployeResponsableVente");
    10. int idPaiement = query.record().indexOf("mode_paiement");
    11. while (query.next())
    12. {
    13. QString name = query.value(idId).toString();
    14. qDebug() << name;
    15. QString magasin = query.value(idmagasin).toString();
    16. qDebug() << magasin;
    17. QString client = query.value(idclient).toString();
    18. qDebug() << client;
    19. QString date = query.value(idDate).toString();
    20. qDebug() << date;
    21. QString employe = query.value(idEmploye).toString();
    22. qDebug() << employe;
    23. QString paiement = query.value(idPaiement).toString();
    24. qDebug() << paiement;
    25.  
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,531
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL commande with QT

    Where is query.exec() ?

  3. #3

    Default Re: SQL commande with QT

    I didn't use any query.exec() untill now, that didn't seem necessary. Where should it be placed ?

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,531
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL commande with QT

    1. Read more about QSqlQuery constructors.
    2. It should be like this :
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("SELECT * FROM commandes WHERE id=(:id)");
    3. query.bindValue(":id", id);
    4. query.exec();
    To copy to clipboard, switch view to plain text mode 

  5. #5

    Default Re: SQL commande with QT

    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

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.