0

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

1
  • You just said in php, you put your user in an array and voilà. Well in C++/Qt, you can also put them in an array. Have a look at QList and QVector to choose which one best fit. In brief, you could create a function QList<QString> testQuery() in which you add each name in the list, then return the list. Have a look at OOP (Oriented Object Programming) and then at C++/Qt. Commented Feb 6, 2015 at 18:06

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for your answer, in the meanwhile i'm using combinations of QMap in QLists or just returning a QMap if i have to return a single item, It seems the cleanest way so far what i have seen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.