PDA

View Full Version : QDataStream and QSql Object. Urgent!! please



ruben.rodrigues
15th August 2011, 11:33
Hi!

I need to pack and unpack a QSqlQuery or a QSqlResult into a QByteArray and using a QDataStream. I need this because later I am supposed to send/received the QByteArray through a tcp socket.

To pack the QSqlQuery I used the result() function:


QByteArray dataBlock;
QDataStream dataStream(&dataBlock, QIODevice::WriteOnly);

dataStream << sqlQuery.result();

return dataBlock;

now when I try to do the opposite I get errors, QDataStream::operator :


QSqlResult *result;
QDataStream dataStream(&qba, QIODevice::ReadOnly);

dataStream >> result; //error

_query = QSqlQuery(result);

is there a solution or even a better way to do this?

thanks

marcvanriet
15th August 2011, 23:39
Have you checked what data is transferred ? I would be surprised if the actual SQL result is in there.

Anyway, since QSqlResult is an abstract interface class, you cannot use >> onto it I think.

See the documentation of QDataStream : "For any (other) Qt class you wnat to use with QDataStream, you have to provide the >> and << operators".

Regards,
Marc

ChrisW67
15th August 2011, 23:57
Stream the row and column count, then iterate through the result set and serialise each row, column by column. You can stream the QVariant values as-is, or convert them to desired types first. At the moment you are serialising the pointer returned by QSqlQuery::result().