Results 1 to 5 of 5

Thread: QsqlQuery to access all the value

  1. #1
    Join Date
    May 2011
    Posts
    39
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default QsqlQuery to access all the value

    Qt Code:
    1. QSqlQuery qry("SELECT time,value FROM customer");
    2. while(qry.next())
    3. {
    4. int at=qry.at();
    5. qDebug()<<"we are at"<<qry.at();
    6. for(int i=0;i<at;i++)
    7. {
    8. qDebug()<<"xvalue"<<_x[i];
    9. _x[i]=qry.value(0).toDouble();
    10. _y[i]=qry.value(1).toDouble();
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QsqlQuery to access all the value

    Hi,

    Why you don't have instead:

    Qt Code:
    1. int at;
    2. at = 0;
    3. int i;
    4. QSqlQuery qry("SELECT time,value FROM customer");
    5. while(qry.next())
    6. {
    7. at++
    8. for(i=0;i<at;i++)
    9. {
    10. qDebug()<<"xvalue"<<_x[i];
    11. _x[i]=qry.value(0).toDouble();
    12. _y[i]=qry.value(1).toDouble();
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by qlands; 5th July 2011 at 15:29.

  3. #3
    Join Date
    May 2011
    Posts
    39
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QsqlQuery to access all the value

    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..
    Last edited by robotics; 5th July 2011 at 15:54.

  4. #4
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QsqlQuery to access all the value

    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:

    Qt Code:
    1. int at;
    2. at = 0;
    3. QSqlQuery qry("SELECT time,value FROM customer");
    4. while(qry.next())
    5. {
    6. at++
    7. _x[at]=qry.value(0).toDouble();
    8. _y[at]=qry.value(1).toDouble();
    9. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QsqlQuery to access all the value

    Quote Originally Posted by qlands View Post
    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:

    Qt Code:
    1. int at;
    2. at = 0;
    3. QSqlQuery qry("SELECT time,value FROM customer");
    4. while(qry.next())
    5. {
    6. at++
    7. _x[at]=qry.value(0).toDouble();
    8. _y[at]=qry.value(1).toDouble();
    9. }
    To copy to clipboard, switch view to plain text mode 
    In code above in both tables element with index 0 will be not set. This code should be :
    Qt Code:
    1. int at;
    2. at = 0;
    3. QSqlQuery qry("SELECT time,value FROM customer");
    4. while(qry.next())
    5. {
    6. _x[at]=qry.value(0).toDouble();
    7. _y[at]=qry.value(1).toDouble();
    8. at++;
    9. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Lesiok for this useful post:

    robotics (6th July 2011)

Similar Threads

  1. Using QSqlQuery
    By darkman_dev in forum Newbie
    Replies: 2
    Last Post: 4th February 2011, 21:40
  2. QSqlQuery
    By yasher in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2010, 14:25
  3. QSqlquery
    By codeman in forum Qt Programming
    Replies: 10
    Last Post: 4th June 2009, 12:57
  4. what is going on a QSqlQuery?
    By mismael85 in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2008, 13:35
  5. QSqlQuery problem
    By dragon in forum Newbie
    Replies: 5
    Last Post: 22nd December 2006, 17:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.