Results 1 to 8 of 8

Thread: How To Get COUNT(*) out in the Query using Values(0)?

  1. #1
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Smile How To Get COUNT(*) out in the Query using Values(0)?

    Hi

    I have problem with this code..
    it does not give me any results.
    even if i try to use .size() it want work as will.

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("SELECT COUNT(*) AS 'numberOfUsers'\n"
    3. " FROM users AS u );
    4. query.exec();
    5.  
    6. if (!query.isActive())
    7. QMessageBox::warning(this, tr("Database Error"),query.lastError().text());
    8.  
    9. numberOfPages = query.value(0).toInt();
    To copy to clipboard, switch view to plain text mode 

    I do not want to use the QSqlTableModel as it slew up all the program.
    I need fast way to get this COUNT number.

    Using Values want work an using Results() did not work as the Handel and .asInt() function in not working as will.

    Regards

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How To Get COUNT(*) out in the Query using Values(0)?

    remove \n from query text.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Smile Re: How To Get COUNT(*) out in the Query using Values(0)?

    Thank you spirit for the vary fast replay..
    I Have toke the \n out of the code..

    its now look like this
    Qt Code:
    1. query.prepare("SELECT COUNT(*) AS 'numberOfUsers' FROM users AS u);
    To copy to clipboard, switch view to plain text mode 
    but id did get any count(*)..

    There is no problem with the SQL statement.

    It just that the line.
    Qt Code:
    1. numberOfPages = query.value(0).toInt();
    To copy to clipboard, switch view to plain text mode 

    Wont gave me the number at all..
    if the SQL Statement has got more than one field back it will work.
    But that will make the program slow and to get something like 500

    Regards

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How To Get COUNT(*) out in the Query using Values(0)?

    did you call QSqlQuery::next before calling QSqlQuery::value?
    do you try to execute this query in Qt's Sqlbrowser which is located in QTDIR/demos/sqlbrowser?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Mr.QT (24th April 2009)

  6. #5
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Smile Re: How To Get COUNT(*) out in the Query using Values(0)?

    Hi spirit,
    WOW it worked. thanks a lot.
    That was fast.

    I have changed the code to this.
    Qt Code:
    1. query.next();
    2. numberOfPages = query.value(0).toInt();
    To copy to clipboard, switch view to plain text mode 
    I think that will not effect the speed..

    But i normally get the query and it work without .next().
    but the problem happened when i have COUNT(*) in the query.

    I did not look at the next function before in the QT Assistant.
    they say :

    The following rules apply:

    If the result is currently located before the first record, e.g. immediately after a query is executed, an attempt is made to retrieve the first record.
    If the result is currently located after the last record, there is no change and false is returned.
    If the result is located somewhere in the middle, an attempt is made to retrieve the next record.
    If the record could not be retrieved, the result is positioned after the last record and false is returned. If the record is successfully retrieved, true is returned.
    Thank you again.


    Best Regards

  7. #6
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How To Get COUNT(*) out in the Query using Values(0)?

    You just need to count the first column in the table, it is faster than counting all columns since they will all have the same value anyway. I think the two "AS" is not necessary.

    I think the following should also work?

    Qt Code:
    1. query.prepare("SELECT COUNT( 1 ) FROM users);
    2. if ( query.exec() && query.seek( 0 ) ) {
    3. numberOfPages = query.value(0).toString().toInt();
    4. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to lni for this useful post:

    Mr.QT (24th April 2009)

  9. #7
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Smile Re: How To Get COUNT(*) out in the Query using Values(0)?

    Hi lni,

    I think it will be faster.
    But why query.seek( 0 ).

    Regards

  10. #8
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How To Get COUNT(*) out in the Query using Values(0)?

    Quote Originally Posted by Mr.QT View Post
    Hi lni,

    I think it will be faster.
    But why query.seek( 0 ).

    Regards
    I think seek( 0 ) is not necessary in this case, and toString() is not necessary either...

Similar Threads

  1. How to realize multi-threaded database query?
    By bangqianchen in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2008, 07:15
  2. QTableWidget Sql Query 25.000 records
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2008, 14:54
  3. Problem with A SQLite Query
    By maveric in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2008, 11:15
  4. Aborting a query set on a TableModel
    By Quid in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2006, 22:36

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.