PDA

View Full Version : Firebird select returns -2 result



guidupas
11th February 2015, 14:13
Hello!

I am facing a problem with a firebird SELECT. When I try to count the number of rows returned it returns the value -2

Does anyone know why?



bool queryRetornoDB::retornaValidadeUsuarioSistema()
{
bool retorno;
//retorno = true;
retorno = false;

QSqlQuery resultadoQuery;

QString query = "SELECT * FROM usuario u";

resultadoQuery.exec(query);

int resultadoQueryNumLinhas = this->retornaQueryNumLinhas(resultadoQuery);

qDebug() << "Valida usuário: ";
qDebug() << resultadoQueryNumLinhas;
}




int queryRetornoDB::retornaQueryNumLinhas(QSqlQuery query)
{
query.last();
int linhas = query.at() + 1;
return linhas;
}


Thanks

Lesiok
11th February 2015, 15:44
You must test values returned from QSqlQuery::exec and QSqlQuery::last.
-2 returned from QSqlQuery::last is QSql::AfterLastRow. This means that the resultset returned from QSqlQuery::exec is empty.

ChrisW67
11th February 2015, 21:22
If you want the number of rows then why not ask the database engine to tell you?
"Select count(*) from Usuario" and then retrieve the single row result.

guidupas
11th February 2015, 22:23
Thanks, it worked