Parse array fields from PostgreSQL
-
Hello to everyone!
Question: is it possible to convert/parse postgreSQL array field types into QList? Or I have to do that manually from QString which I'm getting right now?
Thank you in advance!
-
QSqlQuery neurons;
neurons.exec("SELECT * FROM neurons");
while(neurons.next()) {
qDebug() << neurons.value("weights"); // QVariant(QString, "{0, 0, 255, 0, 255 ... }")
}In PostgreSQL I have this field as int[]. So as you may see it's parsed as QString.
Question is how I can convert it correctly to be QList. Or I have to parse such QString manually? -
@oleg.meleshko said:
Or I have to parse such QString manually?
IMO yes,
the default for unsupported field_type is to return
QString
SO you shoud do something likeQString myStr = neurons.value("weights").toString(); QStringRef sref (myStr, 1, myStr.length() - 2); // Remove braces QVector<QStringRef> v = sref.split(","); QList<int> l; for (const QStringRef &r: v) { l << r.toInt(); }
-
Ok, got it, thank you so much!
-