Count QSqlquery results!!
-
wrote on 7 Jan 2012, 20:18 last edited by
hi to all
I want to count my results of queryfor 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.
-
wrote on 7 Jan 2012, 20:22 last edited by
Is the "size":http://developer.qt.nokia.com/doc/qt-4.8/qsqlquery.html#size method not sufficient?
-
wrote on 7 Jan 2012, 20:27 last edited by
oh i forgot it
so thanks ... -
wrote on 7 Jan 2012, 23:12 last edited by
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.
-
wrote on 8 Jan 2012, 06:31 last edited by
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();}
@ -
wrote on 8 Jan 2012, 11:01 last edited by
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. -
wrote on 8 Jan 2012, 11:50 last edited by
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! -
wrote on 8 Jan 2012, 13:38 last edited by
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
}
@ -
wrote on 8 Jan 2012, 14:06 last edited by
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.
-
wrote on 8 Jan 2012, 14:14 last edited by
hi
thanks Hostel.i tried it.so thanks Andre. Excellent answer.
;) be happy
-
wrote on 8 Jan 2012, 14:24 last edited by
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>
-
wrote on 8 Jan 2012, 14:40 last edited by
[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] -
wrote on 8 Jan 2012, 14:50 last edited by
[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] -
wrote on 8 Jan 2012, 14:56 last edited by
Yeah, there's still a little difference between reading documentation and understanding documentation...
-
wrote on 9 Jan 2012, 05:47 last edited by
hi again
Andre answer me excellent ,that's OK.
why did you reply again?be happy.
thanks all. -
wrote on 17 Jul 2014, 10:43 last edited by
Thanks for this post. I was just wrestling with this problem.
-
wrote on 4 Dec 2014, 09:32 last edited by
For anyone with the same problem, I used to get the rows length with:
@query->record().count()@
I hope may help someone.
-
wrote on 4 Dec 2014, 09:32 last edited by
For anyone with the same problem, I used to get the rows length with:
@query->record().count()@
I hope may help someone.
-
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.
-
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.