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. Getting database values into QStringList
QtWS25 Last Chance

Getting database values into QStringList

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcreatordatabasevalueqstringlist
3 Posts 3 Posters 3.5k 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.
  • L Offline
    L Offline
    Lasith
    wrote on last edited by
    #1

    In my qt c++ application I want to Query a database table and to insert the values into a QStringList!

    Following is my code!

    void Dialog::on_pushButton_clicked()
    {
    QStringList Names;
    if(QSqlDatabase::contains("MyConnection")){

            QSqlQuery qry(QSqlDatabase::database("MyConnection"));
          
    
            qry.prepare("Select NAME from User where NAME like '%Ab%' ");
           qry.exec();
            }
    

    }

    }

    The Query is executed successfully but I dont know how to get the NAME values into the QStringList! How can I achieve it?

    dream_captainD 1 Reply Last reply
    0
    • L Lasith

      In my qt c++ application I want to Query a database table and to insert the values into a QStringList!

      Following is my code!

      void Dialog::on_pushButton_clicked()
      {
      QStringList Names;
      if(QSqlDatabase::contains("MyConnection")){

              QSqlQuery qry(QSqlDatabase::database("MyConnection"));
            
      
              qry.prepare("Select NAME from User where NAME like '%Ab%' ");
             qry.exec();
              }
      

      }

      }

      The Query is executed successfully but I dont know how to get the NAME values into the QStringList! How can I achieve it?

      dream_captainD Offline
      dream_captainD Offline
      dream_captain
      wrote on last edited by dream_captain
      #2

      @Lasith
      you can iterate over results with QSqlQuery::next() and get values with QSqlQuery::value().

      bool prepRet = query.prepare("Select NAME from User where NAME like '%Ab%' ");
      
      if (!prepRet) {
           qDebug() << query.lastError().text();
           return;
      }
      if (!query.exec()) {
           qDebug() << query.lastError().text();
          return;
      }
      QStringList list;
      while (query.next()) {
          list << query.value(0).toString();
      }
      

      EDIT: error reporting

      1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        To add to @dream_captain, in case of an error, at least print the error string from the query so you know what went wrong.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

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