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!



  • Hi e welcome to the devnet,

    could you post an example of how you handle that field now??



  • 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 like

    QString 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!



  • @oleg.meleshko said:

    Ok, got it, thank you so much!

    You're welcome


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.