Count QSqlquery results!!



  • hi to all
    I want to count my results of query

    for Example :
    i have 3 same record in my SQL database in title column
    and i want to search to it. if my titles searched by keyword has one result (do something). else .... (do something)
    @
    QSqlQuery query("SELECT * FROM ... WHERE ... ...");
    query.exec;
    //
    if query has more than one result , (do something)

    // and result was unique
    else {

    }
    @

    thanks.


  • Moderators



  • oh i forgot it
    so thanks ...



  • bq. Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes.

    Not every database reports back the number of rows in a result set. If you want to be database independent you should not rely on this value.



  • why my size always return -1???
    this is my code :
    @
    if (mydb.open())
    {
    QSqlQuery query("SELECT * FROM content WHERE Title like '"+keyword+"'");
    int titleNo = query.record().indexOf("Title");
    int textNo = query.record().indexOf("Text");
    int authorNo = query.record().indexOf("Author");
    while (query.next()) {
    if(query.size()>0){
    title = query.value(titleNo).toString();
    text = query.value(textNo).toString();
    author = query.value(authorNo).toString();
    test = "<h1>" + title + "</h1><br><p>" + text + "</p><br>" + author;}
    }
    else {
    (do somthing);
    }
    mydb.close();

    }
    @



  • About size = -1 see a Volker's post. Maybe your database not support this. When you are using a:
    @
    while( query.next() )
    {
    // code
    }
    @
    Then you have in your query records, so you have not to check the size.



  • I'm using mysqli . support it?
    if no , how can i get the size or number of row in results?

    i used
    @

    query.numRowsAffected()

    @

    too
    but its not worked!



  • You can check if driver support feature by "hasFeature method":http://developer.qt.nokia.com/doc/qt-4.8/qsqldriver.html#hasFeature.

    If you need a result count after while loop then you can do this:
    @
    int recCount = 0;
    while( query.next() )
    {
    recCount++;
    // code
    }
    @



  • If all you need is to distinguish the cases where you have 0, 1 or >1 results (that's what I gather from your opening post), then you don't need to count the results at all. Counting by iterating through the result set is potentially very expensive, so I would avoid it.

    Just perform your query, and try to get the first result. If you don't have any results, you know at this point. If there is a result, cache the values in an object, and try to get the next result. If that works, you have >1 result, if not, you have only 1. No counting needed at all.



  • hi
    thanks Hostel.i tried it.

    so thanks Andre. Excellent answer.

    ;) be happy



  • You should write something:
    @
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    @

    P.S. Use <code>int numRowsAffected()</code>



  • [quote author="abbas farahmand" date="1326023459"]I'm using mysqli . support it?
    if no , how can i get the size or number of row in results?

    i used
    @

    query.numRowsAffected()

    @

    too
    but its not worked![/quote]

    [quote author="tucnak" date="1326032692"]You should write something:
    @
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    @

    P.S. Use <code>int numRowsAffected()</code>[/quote]

    Conclusion:
    [quote]
    I will read previous posts before replying.
    ...
    [/quote]



  • [quote author="tucnak" date="1326032692"]You should write something:
    @
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    I will read documentation.
    @

    P.S. Use <code>int numRowsAffected()</code>[/quote]

    Let me now think about your proposal and read the numRowsAffected documentation:
    [quote]
    Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined. Note that for SELECT statements, the value is undefined; use size() instead.
    [/quote]



  • Yeah, there's still a little difference between reading documentation and understanding documentation...



  • hi again
    Andre answer me excellent ,that's OK.
    why did you reply again?

    be happy.
    thanks all.



  • Thanks for this post. I was just wrestling with this problem.



  • For anyone with the same problem, I used to get the rows length with:

    @query->record().count()@

    I hope may help someone.



  • For anyone with the same problem, I used to get the rows length with:

    @query->record().count()@

    I hope may help someone.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Row length and row count are not the same thing. The row length is the number of field in the row, while the row count is the number of row returned by your query.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Row length and row count are not the same thing. The row length is the number of field in the row, while the row count is the number of row returned by your query.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.