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. [RESOLVED] QSqlQuery point to value

[RESOLVED] QSqlQuery point to value

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.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.
  • M Offline
    M Offline
    mrcgnerd
    wrote on last edited by
    #1

    Hallo, how can I get value from multiple rows selected ?

    If want get value from query I do this:
    @QSqlQuery Query;
    Query.exec("SELECT * FROM Table");
    while (Query.next)
    {
    QString Value = Query.value(0).toString;
    }@

    But what if I my query selects more then one row, how can I select value in second or third row.. ?

    I want to do something like this:

    @Query.exec("SELECT Count(*) FROM Table");
    while (Query.next)
    {
    int Count = Query.value(0).toInteger;
    }

    for (int i = 1; i < =Count ; i++)
    {
    QSqlQuery q;
    q.exec(SELECT * FROM Table);

    QString MyString = q.value(0 Column AND int i Row) // Select value from first column and row = i

    // Do Something with MyString
    }@

    Please help, Thank you.
    Sory for my bad English.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      clochydd
      wrote on last edited by
      #2

      Hi, if I understand your problem you will receive a set of rows by query.exec.
      You may then go through the rows with query.next and extract all columns.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrcgnerd
        wrote on last edited by
        #3

        I Can't get it if I my query select from database select more then one row i can't get my values.

        For example:

        My SQL query selection:

        ID value
        1 x
        2 y

        Now if I use this methot:

        @QSqlQuery Query;
        Query.exec("SELECT * FROM Table");
        while (Query.next)
        {
        QString Value = Query.value(0).toString;
        }@

        I get nothing, because it doesin't know wich row value i want asign to QString.

        How can I do that ?

        Is there a way to use query with tvo parameters ROW and COL

        I want to do something like this:

        Query.valye(0,1);

        Sory for my english

        1 Reply Last reply
        0
        • C Offline
          C Offline
          clochydd
          wrote on last edited by
          #4

          try this:
          @
          query.prepare("SELECT * FROM table;");
          if (!query.exec()) { // make sure your query has been executed successfully
          qDebug() << query.lastError(); // show the error
          } else {
          while (query.next()) {
          // show the colums of each row:
          qDebug() << query.value(0).toString()
          << query.value(1).toString()
          << query.value(3).toString();
          // or: qDebug() << query.value("value").toString()
          // << query.value("x").toString()
          // << query.value("y").toString();

          }
          }
          @

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrcgnerd
            wrote on last edited by
            #5

            Thank you. It was my mistake :

            @QSqlQuery Query;
            Query.exec("SELECT * FROM Table");
            while (Query.next)
            {
            QString Value = Query.value(0).toString;
            Value = "Some text"
            Do something...
            }@

            I don't know why it didnt worked, at first time, but now it's working.

            Anyways thanks for help.

            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