Results 1 to 4 of 4

Thread: How to get value from a query statement ?

  1. #1
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to get value from a query statement ?

    I exec a statement SQL in MYSQL Workbench and result is: 2 ; pic:

    2.jpg

    But in C++ QT I do not know how to get the value like MYSQL, I try this code and result not is: 2

    Qt Code:
    1. void QuanLyDaiLy::on_pushButton_clicked()
    2. {
    3. QSqlQuery *qry = new QSqlQuery();
    4. qry->prepare("select count(MaDaiLy) as SoDaiLyTrongQuan from DaiLy, Quan where DaiLy.Quan_TenQuan = Quan.TenQuan and Quan.TenQuan = 'Quan 10'");
    5. qry->exec();
    6.  
    7. QMessageBox::information(this,"result",QString::number(qry->size()));
    8. }
    To copy to clipboard, switch view to plain text mode 

    1.png

    what can I use to QString::number(qry->????????) to get the value ?

    Thanks for help .

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to get value from a query statement ?


  3. The following user says thank you to Lykurg for this useful post:

    hohoanganh205 (5th January 2012)

  4. #3
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get value from a query statement ?

    Thanks you very much, I done.

    Qt Code:
    1. QSqlQuery q("select count(MaDaiLy) as SoDaiLyTrongQuan from DaiLy, Quan where DaiLy.Quan_TenQuan = Quan.TenQuan and Quan.TenQuan = '" + ui.cbTenQuan->currentText() + "'");
    2. QSqlRecord rec = q.record();
    3. int nameCol = rec.indexOf("SoDaiLyTrongQuan"); // index of the field "name"
    4. q.next();
    5. int SoDLHienCoTrongQuan = q.value(nameCol).toInt();
    To copy to clipboard, switch view to plain text mode 

    the result is return: 2

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to get value from a query statement ?

    Hi, since you only query one "field" the index is 0. Thus you can simplify your code:
    Qt Code:
    1. QSqlQuery q("select count(MaDaiLy) from DaiLy, Quan where DaiLy.Quan_TenQuan = Quan.TenQuan and Quan.TenQuan = '" + ui.cbTenQuan->currentText() + "'");
    2. q.next();
    3. int SoDLHienCoTrongQuan = q.value(0).toInt();
    To copy to clipboard, switch view to plain text mode 
    Further have a look at QSqlQuery::bindValue().

Similar Threads

  1. Help with QT, SQLite, Update Statement
    By chetu1984 in forum Newbie
    Replies: 3
    Last Post: 17th March 2011, 22:24
  2. Replies: 7
    Last Post: 3rd March 2011, 14:32
  3. Replies: 3
    Last Post: 22nd January 2011, 13:08
  4. confusion with a STATEMENT used frequently
    By salmanmanekia in forum Newbie
    Replies: 3
    Last Post: 11th June 2008, 20:54
  5. SmartFOSS Mission Statement
    By travlr in forum Qt-based Software
    Replies: 0
    Last Post: 16th April 2007, 05:41

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.