Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Parse array fields from PostgreSQL

Parse array fields from PostgreSQL

Scheduled Pinned Locked Moved General and Desktop
c++qvariantqlistpostgresql
6 Posts 2 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    oleg.meleshko
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by mcosta
      #2

      Hi e welcome to the devnet,

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

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • O Offline
        O Offline
        oleg.meleshko
        wrote on last edited by oleg.meleshko
        #3

        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?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          @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();
          }
          

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          1
          • O Offline
            O Offline
            oleg.meleshko
            wrote on last edited by
            #5

            Ok, got it, thank you so much!

            M 1 Reply Last reply
            0
            • O oleg.meleshko

              Ok, got it, thank you so much!

              M Offline
              M Offline
              mcosta
              wrote on last edited by
              #6

              @oleg.meleshko said:

              Ok, got it, thank you so much!

              You're welcome

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved