Make Qt applications work with Firebird database.

23 Aug

NB:If you see this post and you like it, please follow https://rongsheng007.wordpress.com. Thx.

In the last post, we have already figure out how to connect to the firebird database in Linux/UNIX environment. In this post, I will use application built in Qt framework to connect the firebird database and do simply task.

To allowing Qt applications connect to the firebird database, we need some tools, here I introduce the IBPP project. IBPP stands for InterBase Plus Plus. IBPP is a client interface written by a group of people which provide connectivity with Interbase database system. Firebird, as is described in previous post, derived from Interbase system. Thus it is natural that IBPP interface support firebird too.

To begin, download the  IBPP source code package from: http://sourceforge.net/projects/ibpp/files/, and unpack to your desired folder. Note that this post is based on the IBPP version 2.5.3.1, all the following configurations are based on this version. You will find two folders in the destination folder where you unpacked the file into. One is “core” and the other is “tests“. (We assume that you already has a Qt project, if not, build one.) Copy all the files from the core folder to your project files location, and put them into a new core folder if you want to. Now open the project with Qt Creator, and add these files into the project.  

[Note that, if you need to install g++ to compile the project. Qt Creator doesn’t seem to provide interfaces to allow user to switch the compiler, be aware of this. OR I haven’t found it yet??]

The first job is to add one line to every IBPP file. (or maybe you have a better idea? I don’t know coz i do this job pure manually…)

What you need to add is:

#include<string.h>    OR   #include<string>

This is very tricky isn’t it? I believed that the dev-group owns their special tool to do the job before compiling…

Okay, after the job is done, we need to configure the project environment. We are using Linux operating system, but you can configure the Qt creator in a similar way in UNIX or more specific: Mac.(See the file in tests folder, it  offers details)

Open the project file(.pro) in the Projects explorer, and add the following in your project file.

QMAKE_CXXOUTPUT = -o
QMAKE_CXXFLAGS += -DIBPP_LINUX
QMAKE_CXXFLAGS += -W -Wall -fPIC
QMAKE_CXXFLAGS += -g -DDEBUG

LIBS +=-lfbclient -lcrypt -lm -ldl -lpthread # dependancies for shared library

Now, your project file should look like this:  

Well, I believed that ALL things are set, what we need to do is to test if it really works. Open a file in your project and try the following way to connect to the database.

First of all, you need to include a header to your project file:

#include “core/ibpp.h”  (if you do not )

then we need to connect to the database using IBPP functions, as follows:

IBPP::Database ibpp = IBPP::DatabaseFactory(“”,”sys.fdb”,”SYSDBA”,”rongsheng”);
ibpp->Connect();

Besides simply do this two lines, add a try-catch block to check if the connection is successfully built. The exception class in IBPP is the IBPP::Exception, and the method for printing error msg out is e.ErrorMessage(), just as follows:

(to make sure the QMessageBox works, include the “QMessageBox.h” header)

try{

…;

}

catch(IBPP::Exception& e)
{
QMessageBox QMsgBox;
QMsgBox.setText(e.ErrorMessage());
QMsgBox.setWindowTitle(“Error”);
QMsgBox.exec();
}

Now run this program, if you assign a blank database file, you could see a error messagebox showing up.

To connect to the database is far way from we can say that everything is done. We still need to see how to retrieve data from the database(we assume that you have already add the connection code):

IBPP::TAM itam = IBPP::amRead;     //set mode to read
IBPP::TIL itil = IBPP::ilConcurrency;  //support concurrency
IBPP::Transaction itran = IBPP::TransactionFactory(ibpp,IBPP::amRead,IBPP::ilConcurrency,IBPP::lrWait,IBPP::TFF(0));    //Initialise the transaction class which defines the transaction procedure.
itran->Start();       //start the transaction.
IBPP::Statement istat = IBPP::StatementFactory(ibpp,itran);   //The class which contains the SQL statement to be executed.
std::string sql = “select * from DICT”;        //our sql statement.
istat->Prepare(sql);  //write the sql statement into the class.
istat->Execute();      //EXECUTE !!!
std::string result;      //we define a string here which used to get the result that returns.
while(istat->Fetch())  //Fetch method is the core method which is used to fetch data from database
{
istat->Get(“DICT_NAME”,result);  //output to result. Be aware that I here only output the COLUMN “DICT_NAME”
}

Now, all the result are written in to ‘result’ variable, you can print it out as you wish. What I’m gonna do is to write them into a QListView control. So firstly I defined a QStringList outside the loop which used to store data:

QStringList list;

then I add:

QString qresult = QString::fromStdString(result);   //convert std::string to QString
list.append(qresult); //append the result into a QStringList

in the while() loop, of course behind the istat statement, and show them into the comtrol with:

QStringListModel* qmodel = new QStringListModel();
qmodel->setStringList(list); //bind the QStringList to the model
ui->listView->setModel(qmodel); //bind the QStringListModel to QListView.
//qmodel is a QListView which we created using Qt Design.

Now, we can really say everything is done! Phew:). Here is the snapshot of result.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: