Results 1 to 4 of 4

Thread: How to extract all datas from a column of a table and add those?

  1. #1
    Join Date
    Jun 2019
    Posts
    5
    Thanks
    1
    Qt products
    Qt5

    Default How to extract all datas from a column of a table and add those?

    I tried to extract data from Amount column of my table and add all those and save them to string. But only first row is extracted,could anyone help?

    Here is my codes:
    Qt Code:
    1. QList<QString> Dbase_Cashier::getAmount()
    2. {
    3. QList<QString> amountdetails;
    4. QSqlQuery qry;
    5. qry.prepare(QString("SELECT Amount FROM Bill"));
    6. if(!qry.exec())
    7. {
    8. qDebug()<<"Error in retriving data"<<qry.lastError();
    9. }
    10. else
    11. {
    12.  
    13. if(qry.next())
    14. {
    15. QString i=qry.value(0).toString();
    16. amountdetails.push_front(i);
    17.  
    18. }
    19. else
    20. {
    21. qDebug()<<"not executing qyery";
    22. }
    23.  
    24. }
    25. qDebug()<<amountdetails.count();
    26. return amountdetails;
    27. }
    To copy to clipboard, switch view to plain text mode 
    And I have called above function as follows:
    Qt Code:
    1. void CashierWindow::showAmount()
    2. {
    3. Dbase_Cashier db("SBS.db");
    4. QList<QString> amountdetails=db.getAmount();
    5.  
    6. if(amountdetails.isEmpty())
    7. {
    8. qDebug()<<"empty";
    9. }
    10. else
    11. {
    12. qDebug()<<amountdetails.count();
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    Here,I got the console output as 1.
    Last edited by anda_skoa; 14th July 2019 at 07:51. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to extract all datas from a column of a table and add those?

    You are only reading the first row, i.e. you are only calling QSqlQuery::next() once.

    Imperative programming languages such as C++ use a concept called loops to run pieces of code multiple times.
    See https://www.tutorialspoint.com/cplusplus/cpp_loop_types for example.

    Part of this concept is to decide the continuation of the loop's execution based on a condition.
    In your case a candidate for such a condition is the return value of QSelQuery::next().

    Cheers,
    _

  3. #3
    Join Date
    Jun 2019
    Posts
    5
    Thanks
    1
    Qt products
    Qt5

    Default Re: How to extract all datas from a column of a table and add those?

    I actually don't understand what you mention?

    How many times do I execute my loop for?

    How do I know how many rows are there in that column,could you please help me with it?

    How can I count no. of rows in that column?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to extract all datas from a column of a table and add those?

    Qt Code:
    1. while(qry.next())
    2. {
    3. QString i=qry.value(0).toString();
    4. amountdetails.push_front(i);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    RamrajCh (14th July 2019)

Similar Threads

  1. Column/Row no of ComboBox Widget in Table
    By ankurjain in forum Qt Programming
    Replies: 13
    Last Post: 6th December 2011, 11:03
  2. how to add a image to table column header
    By nageshvk in forum Newbie
    Replies: 3
    Last Post: 25th December 2010, 13:34
  3. Listing a column of a table to a QListWidget
    By Luc4 in forum Qt Programming
    Replies: 1
    Last Post: 2nd October 2010, 20:21
  4. Replies: 2
    Last Post: 5th September 2010, 14:06
  5. Table Column Problem
    By kenny_isles in forum Newbie
    Replies: 1
    Last Post: 5th March 2007, 23:35

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.