Parse array fields from PostgreSQL
-
wrote on 26 Mar 2015, 23:21 last edited by
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!
-
wrote on 26 Mar 2015, 23:50 last edited by mcosta
Hi e welcome to the devnet,
could you post an example of how you handle that field now??
-
wrote on 26 Mar 2015, 23:54 last edited by oleg.meleshko
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? -
wrote on 27 Mar 2015, 00:06 last edited by
@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(); }
-
wrote on 27 Mar 2015, 00:20 last edited by
Ok, got it, thank you so much!
-
Ok, got it, thank you so much!
wrote on 27 Mar 2015, 00:21 last edited by
1/6