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.

Set up and Connect to the Firebird Database.

22 Aug

[NB]: please follow up the blog at : https://rongsheng007.wordpress.com if you are reading the article in facebook or other places. Thx.

Firebird database is a database based on the Borland Interbase database. Firebird provides powerful features both on it’s server edition and embedded edition. I personally use this database in desktop application development. This software becomes my first choice since it is free and softwares can access database file without building up the server.(embedded version).

BTW: Firebird database seems to support a variety of operating systems.

Here’s how we do it in Linux/UNIX.

1. choose the edition and download the package of firebird database from sourceforge (currently 2.5 RC3)

http://www.firebirdsql.org/index.php?op=files&id=fb250_rc3

2. Unpack the file to a folder (say ~/Document/)

3. Run scripts
for Ubuntu and it’s derived system: $ sudo sh ./install.sh
for other linux/unix, first switch to root user using
# su
then #sh ./install.sh

4. Configuration
go to the folder /opt/firebird/ , run
for Ubuntu $sudo passwd firebird
for other linux/unix: #su passwd firebird
then set the new UNIX password.

5. Test
Now you should be in the folder: /opt/firebird , if you are not in this folder, please switch to this folder.
run the following command as root:
./bin/isql
you should see:

Use CONNECT or CREATE DATABASE to specify a database
SQL>

now try the sql command:
connect ./examples/empbuild/employee.fdb user SYSDBA password ;

note: replace to the password you just set and do not ignore the ‘;’.

now you may see:
Database: ./examples/empbuild/employee.fdb, User: SYSDBA

try another command: show table; (don’t omit the ‘;’)

you may see:
COUNTRY                                      CUSTOMER
DEPARTMENT                           EMPLOYEE
EMPLOYEE_PROJECT        JOB
PROJECT                                       PROJ_DEPT_BUDGET
SALARY_HISTORY SALES

till now, you have successfully configured the firebird database, use “quit;” to exit the command prompt.

Running the Server:

If you want to start the sever rather than using files to open it, you need to start the fbguard session.

open a terminal and direct to folder :  “/opt/firebird/bin/”, then run the following command as root user.

./fbguard             or                ./fbguard -d   (if you want to run it in background)

now the system is started, you can run a GUI tool to examine, try “FlameRobin”. here is a screenshot of the configured firebird database.

Pictures only V2

22 Jul

some pictures about my university. Take a look.

Pictures only

15 Jul

the above pictures are taken in Kuala Lumper International Airport, Malaysia; the places where i lived and the Woolworth Supermarket.

Baba yetu

8 Jul

The theme music for video game – Civilization IV, written and compsed by Christopher Tin.Many friends asked me why you are so fond of the game Civ4. The theme song could be counted as the main reason, sometimes, i keep the game in the main screen instead of clicking ‘Play Now!’ just to re-enjoy the music.

Vodpod videos no longer available.
Music, posted with vodpod

Shifting again.

6 Jul

I have decided to settle here, in WordPress, for unknown reasons, possibly just want something fresh. Windows Live Blog is deprecated since now.

The first impression of WordPress.com is kind of “WOW”, while the background console is much toooooo slow. The whole background console in written in a Mac-Style way, which makes it quite comfortable. Whatever, this shall be my new home.

Yet, this site is still under construction.