comlink
comlink

Reputation: 27

Returning SQL Data from functions in QT C++

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++.

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.

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).

some examples how my code is now:

main.cpp

#include <QCoreApplication>
#include <QDebug>
#include <QSqlDatabase>
#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();
}

database_mysql.h

#ifndef DATABASE_MYSQL_H
#define DATABASE_MYSQL_H
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlQuery>
#include <QtDebug>

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 

database_mysql.cpp

#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlQuery>
#include <QtDebug>
#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() << "Not Connectd";
    }else{
        qDebug() << "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() << "Database Not Connected!";
    }else{
        QSqlQuery query;
        query.exec("SELECT * FROM sys_user");
        while (query.next()){
            QString name = query.value(1).toString();
            qDebug() << "Name: " << name;
        }
        result = true;
    }
    return result;
}

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.

Many Thanks,

Ben

Upvotes: 0

Views: 1445

Answers (1)

transistor
transistor

Reputation: 669

As Martin wrote, you can just use an array or list. Here's a sample with QList. I choosed to hand the list over as a reference parameter, so you still have the bool as return value as success indicator.

main.cpp

#include <QCoreApplication>
#include <QDebug>
#include <QSqlDatabase>
#include <QList>
#include "database_mysql.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    database_mysql db;

    QList<QString> names;

    db.test_query(names);

    for_each(QString name, names){
      qDebug()<< "Name: " << name;
    }

    return a.exec();
}

database_mysql.h

#ifndef DATABASE_MYSQL_H
#define DATABASE_MYSQL_H
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlQuery>
#include <QtDebug>

class database_mysql
{

    QSqlDatabase m_database;



public:
    database_mysql();
    ~database_mysql();

    bool connect();
    void disconnect();
    //This should not be a bool...
    bool test_query(QList<QString>& namesList);

};

#endif // DATABASE_MYSQL_H 

database_mysql.cpp

#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlQuery>
#include <QtDebug>
#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() << "Not Connectd";
    }else{
        qDebug() << "Database Connected";
        result = true;
    }
    return result;
}


void database_mysql::disconnect()
{
 if (m_database.open())
 {
     m_database.close();
 }
}


bool database_mysql::test_query(QList<QString>& namesList)
{
    bool result = false;
    namesList.clear();//clear possible old entries

    if(!connect()){
        qDebug() << "Database Not Connected!";
    }else{
        QSqlQuery query;
        query.exec("SELECT * FROM sys_user");
        while (query.next()){
            QString name = query.value(1).toString();
            namesList.append(name);
        }
        result = true;
    }
    return result;
}

Upvotes: 1

Related Questions