PDA

View Full Version : Retrieve single record from QSqlQuery



mustermann.klaus@gmx.de
10th March 2018, 18:39
Hallo,

I do



qDebug() << "ID" << id;
qDebug() << query->isActive() << query->isSelect() << query->seek(id, false) << query->record();)

and I retrieve



ID 75
true true true QSqlRecord(29)

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


query->next()

it was better to say



query->seek(id)

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



qDebug() << "ID" << id;
qDebug() << query->isActive() << query->isSelect() << query->seek(id, false) << query->at();

returns



ID 74
true true true 110

mustermann.klaus@gmx.de
11th March 2018, 14:44
Okay, as a workaround, and just to see, I'm on the right way, I can use



// wanted style
qDebug() << "ID" << id;
qDebug() << query->isActive() << query->isSelect() << query->seek(id, false) << query->at();

// workaround style
query->first();
for(int i = 0; i<id; i++){
query->next();
}
qDebug() << query->isActive() << query->isSelect() << query->at();


which returns



ID 57
true true true 89
true true 57


What am I doing wrong here?

regards, Lars

d_stranz
11th March 2018, 16:39
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.

mustermann.klaus@gmx.de
11th March 2018, 17:50
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

d_stranz
12th March 2018, 00:52
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).