PDA

View Full Version : SQLQuery->first() not working



Momergil
25th October 2012, 17:56
Hello!

I'm developing a software where I need to do some queries in a MySQL server. I created a C++ class where functions are used with a global query to call the data from the server and than be used in the app.

Till now everything was fine and I managed to do some code with the query working well, till I got a specific case where, for reasons I can't comprehend, the query is always returning "false" for the first() function. Here is the code:



bool SQLFunctions::pesquisaTabelaTendencia(QString passagem, int inicio, int fim)
{
if (global_query->exec("SELECT DATE_FORMAT(ev_datetime, '%d-%m-%Y'), TIME(ev_datetime),"
"EV_VALOR_ECG, EV_VALOR_RESP, EV_VALOR_OXI, EV_VALOR_TEMP1, EV_VALOR_TEMP2, EV_VALOR_PI1, EV_VALOR_PI2, "
"EV_VALOR_PNI, EV_VALOR_CAP, EV_VALOR_ST_I, EV_VALOR_ST_II, EV_VALOR_ST_III, EV_VALOR_ST_aVR, EV_VALOR_ST_aVL, EV_VALOR_ST_aVF, EV_VALOR_ST_V "
"FROM tendencia_1_5 "
"WHERE pt_passagem_prontuario = '" + passagem + "' "
"ORDER BY ev_datetime DESC LIMIT " + QString::number(inicio) + ", " + QString::number(fim)))
{
qDebug() << "Deu certo" << global_query->isActive() << global_query->isValid() << global_query->isSelect();
global_query->first();
qDebug() << "Deu certo" << global_query->isActive() << global_query->isValid() << global_query->isSelect();
if (global_query->first())
return true;
else
return false;
}
}


Actually this functions is a little bit different to the one I'm actually wanting tu use, since I modified it to do my research in the problem.

So the results are that the exec() function works very well, but the first() function always return false (isActive() and isSelect() also returns true while isValid() always returns false, as one would expect given that first() doesn't work).

It's good to remember that the class has similar functions that only have the QString in the exec() function different, and it has performed very nice till now; the unique problem happens with this function, where the first() returns false and I can't see why. Also I'm sure that the sql select statement is correct, since it was taken from another software that works fine with it.

Does anyone have any explanation?


Thanks,

Momergil

Lesiok
25th October 2012, 18:25
Looks like an empty result set. Are you sure that the query will return any records ?

Momergil
25th October 2012, 19:51
Looks like an empty result set. Are you sure that the query will return any records ?

Yes. Performing the same query with that other working software I mentioned several data is found.

How can I see if it's the case of an 'empty result set'?

---
Complementing:

I tryied using QSQLDatabase::lastError() and QSqlQuery::lastError(), but nothing appeared.

Added after 5 minutes:

Actually, you're right! The first paramenter was being passed empty because I missed a line of code I though I had already written :P

Thanks very much,

Momergil

wysota
25th October 2012, 20:39
It's better to use hasNext() and next() rather than first().