title: Returning SQL Data from functions in QT C++
body: <p>I'm a very very beginner in C++ (QT), normally i used to work with the PHP/MYSQL for web, Python for server tasks etc..., i want to learn C++ and i have a project on my work that needs a daemon, and some clients who talks to each other, i want to create this in C++.</p>
  
  <p>I was searching for some examples for handling SQL returned DATA, i was creating function like get_user(); get_users(); etc..., and i should do something with this, in PHP i just had to put them in an array and voila, returned.</p>
  
  <p>What is the best practice in QT C++ to do that? in the many examples on the web they all do it in the main function, so this didn't help me very much, my Database transactions would be in a separated header/source file (class).</p>
  
  <p>some examples how my code is now:</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;QCoreApplication&gt;
  #include &lt;QDebug&gt;
  #include &lt;QSqlDatabase&gt;
  #include "database_mysql.h"
  
  int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
  
      database_mysql db;
  
      // should have here an object or an array so that i could work with it...
  
      db.test_query();
  
      return a.exec();
  }
  </code></pre>
  
  <p><strong>database_mysql.h</strong></p>
  
  <pre><code>#ifndef DATABASE_MYSQL_H
  #define DATABASE_MYSQL_H
  #include &lt;QtSql/QSql&gt;
  #include &lt;QtSql/QSqlDatabase&gt;
  #include &lt;QtSql/QSqlDriver&gt;
  #include &lt;QtSql/QSqlQuery&gt;
  #include &lt;QtDebug&gt;
  
  class database_mysql
  {
  
      QSqlDatabase m_database;
  
  
  
  public:
      database_mysql();
      ~database_mysql();
  
      bool connect();
      void disconnect();
      //This should not be a bool...
      bool test_query();
  
  };
  
  #endif // DATABASE_MYSQL_H 
  </code></pre>
  
  <p><strong>database_mysql.cpp</strong></p>
  
  <pre><code>#include &lt;QtSql/QSql&gt;
  #include &lt;QtSql/QSqlDatabase&gt;
  #include &lt;QtSql/QSqlDriver&gt;
  #include &lt;QtSql/QSqlQuery&gt;
  #include &lt;QtDebug&gt;
  #include "database_mysql.h"
  
  
  // Constructer
  database_mysql::database_mysql()
  {
  
  }
  
  
  // Desctructer
  database_mysql::~database_mysql()
  {
      disconnect();
  }
  
  
  bool database_mysql::connect()
  {
      bool result = false;
      // Connect the database, for the moment still static defined
      QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
      db.setHostName("localhost");
      db.setDatabaseName("anna");
      db.setUserName("anna");
      db.setPassword("anna");
      if(!db.open()){
          qDebug() &lt;&lt; "Not Connectd";
      }else{
          qDebug() &lt;&lt; "Database Connected";
          result = true;
      }
      return result;
  }
  
  
  void database_mysql::disconnect()
  {
   if (m_database.open())
   {
       m_database.close();
   }
  }
  
  
  bool database_mysql::test_query()
  {
      bool result = false;
      if(!connect()){
          qDebug() &lt;&lt; "Database Not Connected!";
      }else{
          QSqlQuery query;
          query.exec("SELECT * FROM sys_user");
          while (query.next()){
              QString name = query.value(1).toString();
              qDebug() &lt;&lt; "Name: " &lt;&lt; name;
          }
          result = true;
      }
      return result;
  }
  </code></pre>
  
  <p>I really searched the web for this, its very hard to find some of this kind of information, if you have this kind of information, a hint/url would be very appreciated.</p>
  
  <p>Many Thanks,</p>
  
  <p>Ben</p>