PDA

View Full Version : QsqlQuery to access all the value



robotics
5th July 2011, 16:02
QSqlQuery qry("SELECT time,value FROM customer");
while(qry.next())
{
int at=qry.at();
qDebug()<<"we are at"<<qry.at();
for(int i=0;i<at;i++)
{
qDebug()<<"xvalue"<<_x[i];
_x[i]=qry.value(0).toDouble();
_y[i]=qry.value(1).toDouble();
}
}
here I am trying to access the data in array x and y i.e. all the value in these array but the problem is I am getting only the last value in my array i.e. for example my last value is 8 then i am getting all value 8 in my array
I couldn't access all the value in array from database....
please help me

qlands
5th July 2011, 16:14
Hi,

Why you don't have instead:



int at;
at = 0;
int i;
QSqlQuery qry("SELECT time,value FROM customer");
while(qry.next())
{
at++
for(i=0;i<at;i++)
{
qDebug()<<"xvalue"<<_x[i];
_x[i]=qry.value(0).toDouble();
_y[i]=qry.value(1).toDouble();
}
}


Assuming that lists _x and _y are big enough to the the whole data.
A note.. Look that I starts in 0 in each record... thus new record replaces old values in the lists. That is way you see the last value in all the positions of the list.

robotics
5th July 2011, 16:46
thank you for you reply but that also did not solve my problem........still the same problem as above
I am getting only the last value in the array
I want to get all the value from the database
QSqlQuery is not positioned from the first record instead it is positioned at the last record ..
I am trying to use loop to get all the records...but instead I am positioned at the last record..:(

qlands
5th July 2011, 18:17
Hi,

In your code. For each record in the while loop you are storing each cell of the list with the value of that current record. If you want to store all the values in those strings I suggest to change the code to:



int at;
at = 0;
QSqlQuery qry("SELECT time,value FROM customer");
while(qry.next())
{
at++
_x[at]=qry.value(0).toDouble();
_y[at]=qry.value(1).toDouble();
}

Lesiok
6th July 2011, 11:19
Hi,

In your code. For each record in the while loop you are storing each cell of the list with the value of that current record. If you want to store all the values in those strings I suggest to change the code to:



int at;
at = 0;
QSqlQuery qry("SELECT time,value FROM customer");
while(qry.next())
{
at++
_x[at]=qry.value(0).toDouble();
_y[at]=qry.value(1).toDouble();
}

In code above in both tables element with index 0 will be not set. This code should be :


int at;
at = 0;
QSqlQuery qry("SELECT time,value FROM customer");
while(qry.next())
{
_x[at]=qry.value(0).toDouble();
_y[at]=qry.value(1).toDouble();
at++;
}