PDA

View Full Version : Passing QsqlRecords in a function



ambarish_singh
13th August 2009, 09:09
Hi all,

I am quite new to QT programming. The problem is I want to transfer sql records which I get from firing select query in my database file to another class. I'm facing problems in doing so. Can anyone please help!!!!


thanks
Ambarish.

yogeshgokul
13th August 2009, 09:12
How you are trying? QSqlRecord or something else.

ambarish_singh
13th August 2009, 10:26
Hi yogesh,
Thanks for the quick reply.

Here is how am I using QSqlRecord in my function.

QSqlQuery q;
QSqlRecord record;
q.prepare(QLatin1String("select * from calendarentry where date = ?"));
q.addBindValue(date);
if(!q.exec())
{
qDebug()<<"Error while executing select Query";
qDebug()<<q.lastError();
}
else
{
qDebug() << "Select Query fired Successfully on database for date in fetch day entries" << date;
while(q.next())
{
qDebug()<<"Entries in the table are"<<q.value(0).toInt();
qDebug()<<"Entries in the table are"<<q.value(1).toString();
qDebug()<<"Entries in the table are"<<q.value(2).toString();
qDebug()<<"Entries in the table are"<<q.value(3).toString();
qDebug()<<"Entries in the table are"<<q.value(4).toString();
qDebug()<<"Entries in the table are"<<q.value(5).toString();
qDebug()<<"Entries in the table are"<<q.value(6).toString();
}
record = q.record();
return (record);

Now how to fetch field data from this record or is there any other way......

And ya liked your tagline " daru vich pyar"

Thanks,
Ambarish

yogeshgokul
13th August 2009, 10:44
Now how to fetch field data from this record or is there any other way......

Very Easy , Use:

QSqlField QSqlRecord::field ( const QString & name );


And ya liked your tagline " daru vich pyar"
Have you ever mixed? ;)

ambarish_singh
13th August 2009, 11:20
sorry man. I'm still not able to get the field values.

qfield = record.field("date");
qDebug()<<"date record is"<<qfield.value().toString();
or
qDebug()<<"date record is"<<qfield;
the output is "date record is QVariant(, ) "

Have you ever mixed?
ya sometimes:)

Thanks
Ambarish

yogeshgokul
13th August 2009, 11:37
Use code tags for pasting code lines.

Can you try this and see the result.

qfield = record.field("date");
if(qfield.isValid()){
qDebug()<<"Field is valid named : "<<qfield.name();
qDebug()<<"Field value is :"<<qfield.value().toString();
}
else
qDebug()<<"Field is invalid ";

ambarish_singh
13th August 2009, 11:58
Hi ,

This is the output I'm getting:

Field is valid named : "date"
Field value is : ""


Thanks,
Ambarish:confused:

ambarish_singh
13th August 2009, 12:10
gotcha!!!!

thanks for your valuable help!!!!!