I have a QList list. I want to insert it on the database. I didn't find any serializer method after some googling. If there any method / idea to serialize the list data for database?
Asked
Active
Viewed 4,067 times
3
-
1If you mean a relational database, then the best way is not try to serialize it at all. That's what you have tables and columns for. – Lukáš Lalinský Mar 22 '12 at 07:18
-
Are you planning to serialize the whole list and then insert it into a specific column? Please avoid it as it would be a really bad design. – MD Sayem Ahmed Mar 22 '12 at 07:20
-
You didn't find this? http://lists.qt.nokia.com/pipermail/qt-interest/2011-July/035068.html – Mark Fraser Mar 22 '12 at 07:22
-
Of course it's bad design. But he wasn't asking about design. – Mark Fraser Mar 22 '12 at 07:23
-
@SayemAhmed Yeah! But I'm already ahead with some codings. I need to finish it. – Dewsworld Mar 22 '12 at 07:23
-
1@Dewsworld: Did the solution work ? – MD Sayem Ahmed Mar 22 '12 at 09:02
-
@Dewsworld: What is going wrong? How are you inserting it into the database? Are you facing problems while serializing it? – MD Sayem Ahmed Mar 25 '12 at 07:03
-
1@SayemAhmed When I'm fetching them with a **query** I get a size 0 for the data I've inserted with Streaming. Btw, I've change the insertion with a conversion from StringList to String, it's a bit ugly, but currently serving my purpose. – Dewsworld Mar 25 '12 at 09:14
2 Answers
3
How about using QStringList
instead of QList<QString>
-
QStringList numberList_; // instead of QList<QString>, use this
QString myString1 = "Hello";
QString myString2 = "World";
numberList_ << myString1;
numberList_ << myString2;
QByteArray byteArray;
QBuffer buffer(&byteArray);
QDataStream out(&buffer);
out << numberList_;
Probably QList<QString>
should also work in place of QStringList
. If it doesn't, well, you can convert it pretty easily to QStringList
.
QDataStream, QBuffer, QByteArray and QStringList reference.

MD Sayem Ahmed
- 28,628
- 27
- 111
- 178
0
Here is another option that is a bit more succinct:
QString serialize(QStringList stringList)
{
QByteArray byteArray;
QDataStream out(&byteArray, QIODevice::WriteOnly);
out << stringList;
return QString(byteArray.toBase64());
}
QStringList deserialize(QString serializedStringList)
{
QStringList result;
QByteArray byteArray = QByteArray::fromBase64(serializedStringList.toUtf8());
QDataStream in(&byteArray, QIODevice::ReadOnly);
in >> result;
return result;
}

TilmanK
- 103
- 3

Cory Klein
- 51,188
- 43
- 183
- 243