PDA

View Full Version : Class to connect and make a query on a database



junix
8th May 2010, 19:31
Hi everyone,

I'd like to create a simple class to run my queries in my program.

The sctructure is:

---------------------------------------------
::: libdb.h
---------------------------------------------


#ifndef LIBDB_H
#define LIBDB_H

#include <QSqlDatabase>
#include <QSqlQuery>
#include <QString>
#include <QDebug>

class DataBase
{
public:
DataBase();
virtual ~DataBase();
QSqlQuery setQuery(QString);
};

#endif // LIBDB_H


---------------------------------------------
::: libdb.cpp
---------------------------------------------


#include "libdb.h"

DataBase::DataBase()
{
}

DataBase::~DataBase()
{
}

QSqlQuery setQuery(QString sql_statement)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("10.0.2.2");
db.setDatabaseName("testdb");
db.setUserName("root");
db.setPassword("password");
if (!db.open()) {
qDebug() << "database wasn't opened";
return NULL;
} else {
QSqlQuery sqlquery(sql_statement, db);
sqlquery.prepare(sql_statement);
sqlquery.exec();
db.commit();
db.close();
return sqlquery;
}
}


---------------------------------------------
::: mainwindow.cpp
---------------------------------------------


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "libdb.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

DataBase mDB = DataBase();
QSqlQuery query;
query = mDB.setQuery("SELECT * FROM alarm");
while(query.next()) {
QString alarm = query.value(0).toString();
qDebug() << "Alarms: " << alarm;
}
}

But, I've gotting the error:
undefined reference to `Database::setQuery(QString)' mainwindow.cpp13
mainwindow.cpp13 = 'query = mDB.setQuery("SELECT * FROM alarm");'

What I'm doing wrong?

Thanks.

tbscope
8th May 2010, 20:32
Easy:

QSqlQuery setQuery(QString sql_statement)

should be:

QSqlQuery Database::setQuery(QString sql_statement)

junix
8th May 2010, 22:26
Thanks tbscope, now it can compile, but, now I've got another error:


QSqlDatabase: QPSQL driver not loaded

QSqlDatabase: available drivers: QSQLITE QODBC3 QODBC

How can I install QPSQL driver?

Lykurg
8th May 2010, 22:48
How can I install QPSQL driver?
By reading the documentation! See "SQL Database Drivers". And it is a bad idea to set up a database every time DataBase::setQuery() is called. Qt stores a connection, so there is no need to set it up again and again.

junix
8th May 2010, 23:06
Ok.
I'm downloading the PostgreSQL for Windows to compile the plugin.

So you think that is necessary only once to initialize QSqlDatabase?

The class should be only to make queries? In this case I can not close the connection, right?


db.close();

junix
9th May 2010, 02:44
I've tryed to build QPSQL driver, but... I'm using MinGW.
Following the QT Wiki documentation, I downloaded all of MinGW-Tools package and there is no reimp tool inside!
I found a link to reimp tool made by Anders Norlander, but his site is off.
After some searchs, I found another guy called Jose Fonseca and I got the reimp tool, but only sources files that I couldn't build.

Maybe there is another way to build or download QPSQL driver?

Somebody has the driver and can put available to download?

Thanks.

junix
9th May 2010, 06:37
Finnaly after 8 hours trying to build this driver, it's done!
On Windows 7 it is impossible! Only it's works on XP.
I build on XP and copy to Win 7.

Can I share this .dll with another people?

Lykurg
9th May 2010, 10:35
Can I share this .dll with another people?I don't know. Try if it works. E.g. set up a virtual box or simply try it on an other computer.
As for the database question. Of course you then shouldn't close it. see QSqlDatabase::addDatabase() and it second parameter! and for the query simply use
QSqlQuery q(QSqlDatabase::database("YourConnectionName"));

junix
9th May 2010, 16:43
Yes, it works!
I build on XP and copy to Windows 7 and both of them is working.
I'd like to share this dll's, if somebody knows if is possible, please, tell me.

Thanks.