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. QSqlQuery, how to iterate through a record?
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery, how to iterate through a record?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.3k Views 1 Watching
  • 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I am using QSqlQuery to perform a query, previously I have supplied an array of field names, however I want to replace this by iterating through a record and getting each field automatically.

    I've searched online and cannot find an example, how do I achieve this?

    Kind Regards,
    Sy

    jsulmJ KroMignonK 2 Replies Last reply
    0
    • SPlattenS SPlatten

      I am using QSqlQuery to perform a query, previously I have supplied an array of field names, however I want to replace this by iterating through a record and getting each field automatically.

      I've searched online and cannot find an example, how do I achieve this?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @SPlatten Right there in documentation: https://doc.qt.io/qt-5/qsqlquery.html

      QSqlQuery query("SELECT country FROM artist");
          while (query.next()) {
              QString country = query.value(0).toString();
              doSomething(country);
          }
      

      With query.value(INDEX) you access the field INDEX. You can also use https://doc.qt.io/qt-5/qsqlquery.html#value-1 to access columns by name...

      If you mean you don't want to hard code the column names, but instead make it generic you can use "DESCRIE table_name" SQL command to get information about the table.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      5
      • SPlattenS SPlatten

        I am using QSqlQuery to perform a query, previously I have supplied an array of field names, however I want to replace this by iterating through a record and getting each field automatically.

        I've searched online and cannot find an example, how do I achieve this?

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by KroMignon
        #3

        @SPlatten said in QSqlQuery, how to iterate through a record?:

        previously I have supplied an array of field names, however I want to replace this by iterating through a record and getting each field automatically.

        I don't really understand what your are talking about?
        Do you want to know position of each field of your requests or loop through each record?

        If you want to get position of each field you can you QSqlQuery::record():
        for example:

        if(query.exec("SELECT * from position WHERE ID > 100"))
        {
            const auto rec = query.record();
            const int col_X = rec.indexOf("X");
            const int col_Y = rec.indexOf("Y");
            while(query.next())
            {
                int X = query.value(col_X).toInt();
                int Y = query.value(col_Y).toInt();    
            }
        }
        

        For more details see documentation ==> https://doc.qt.io/qt-5/qsqlquery.html#record

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        1
        • SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #4

          Thank you, I'll take a look.

          Kind Regards,
          Sy

          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