Results 1 to 5 of 5

Thread: Retrieve single record from QSqlQuery

  1. #1
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Retrieve single record from QSqlQuery

    Hallo,

    I do

    Qt Code:
    1. qDebug() << "ID" << id;
    2. qDebug() << query->isActive() << query->isSelect() << query->seek(id, false) << query->record();)
    To copy to clipboard, switch view to plain text mode 

    and I retrieve

    Qt Code:
    1. ID 75
    2. true true true QSqlRecord(29)
    To copy to clipboard, switch view to plain text mode 

    Why is that?

    My aim is to retrieve a specific record at a known ID. My approach was all easy: thought instead of digging through all the query using

    Qt Code:
    1. query->next()
    To copy to clipboard, switch view to plain text mode 

    it was better to say


    Qt Code:
    1. query->seek(id)
    To copy to clipboard, switch view to plain text mode 

    But obviously I don't understand the methode. Anybody here to help me out?

    Thanks, Lars



    Added after 8 minutes:


    update: ok, 29 is the length of the record I try to readout.

    But what about

    Qt Code:
    1. qDebug() << "ID" << id;
    2. qDebug() << query->isActive() << query->isSelect() << query->seek(id, false) << query->at();
    To copy to clipboard, switch view to plain text mode 

    returns

    Qt Code:
    1. ID 74
    2. true true true 110
    To copy to clipboard, switch view to plain text mode 
    Last edited by mustermann.klaus@gmx.de; 10th March 2018 at 19:37.

  2. #2
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Retrieve single record from QSqlQuery

    Okay, as a workaround, and just to see, I'm on the right way, I can use

    Qt Code:
    1. // wanted style
    2. qDebug() << "ID" << id;
    3. qDebug() << query->isActive() << query->isSelect() << query->seek(id, false) << query->at();
    4.  
    5. // workaround style
    6. query->first();
    7. for(int i = 0; i<id; i++){
    8. query->next();
    9. }
    10. qDebug() << query->isActive() << query->isSelect() << query->at();
    To copy to clipboard, switch view to plain text mode 

    which returns

    Qt Code:
    1. ID 57
    2. true true true 89
    3. true true 57
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong here?

    regards, Lars

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Retrieve single record from QSqlQuery

    Try the sequence exec(), next(), then seek(). It may be that whatever DB driver you are using requires that the cursor be positioned at the first record before a seek will work, despite what the documentation seems to say.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Retrieve single record from QSqlQuery

    Oh, hallo d_stranz again,

    Yes. that worked. I added a simple query->frst() followed by the seek() thing. Due to threading-issues I can not call exec() from that point.

    thanks so much, Lars

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Retrieve single record from QSqlQuery

    Due to threading-issues I can not call exec() from that point.
    No, I meant that you should exec() your query (which you did already, because isActive() and isSelect() are both true), then before trying to seek() you should call next() (or first(), since that seems to work for you).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QSqlQuery: remove a record
    By Evgenij in forum Qt Programming
    Replies: 1
    Last Post: 16th May 2014, 23:26
  2. Replies: 2
    Last Post: 9th April 2013, 10:15
  3. QSqlQuery escaping single quotes
    By pdoria in forum Qt Programming
    Replies: 0
    Last Post: 1st April 2012, 22:37
  4. Replies: 2
    Last Post: 25th January 2011, 11:17
  5. Replies: 4
    Last Post: 23rd October 2010, 18:54

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.