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
Forum Updated to NodeBB v4.3 + New Features

Getting database values into QStringList

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcreatordatabasevalueqstringlist
3 Posts 3 Posters 3.5k 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.
  • L Offline
    L Offline
    Lasith
    wrote on 30 Nov 2017, 05:40 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?

    D 1 Reply Last reply 30 Nov 2017, 05:44
    0
    • L Lasith
      30 Nov 2017, 05:40

      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?

      D Offline
      D Offline
      dream_captain
      wrote on 30 Nov 2017, 05:44 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
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 30 Nov 2017, 06:17 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

        1/3

        30 Nov 2017, 05:40

        • Login

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