Using PostgreSQL, C++ and curses - Table of Contents
Introduction
A while back, I started playing around with PostgreSQL, a free database that is available for Linux. I work with SQL Server 6.5 in my professional life and wanted to find an alternative that could possibly be used in its place.
Though I haven't yet gotten to that point, I have been pleasantly surprised to find PostgreSQL easy to work with.
Some background. In a previous job, I worked with Informix which provides two interesting tools - a screen writer and a report writer. The screen writing tool is particularly interesting. Just point it at a table and it will build a simple data entry screen. The 'language' used is fairly simple and at the same time, powerful. The report writer is equally simple and powerful.
I missed the ease of building screens I had with Informix. PostgreSQL does not, to my limited knowledge, offer the same facility. There may be tools out on the net which provide this. I simply don't know about them.
Frustration led me to my comfort zone, the C programming language. I figured I would have to write my own. Early on, I decided to try using C++ and it has been a wonderful journey. C++ has surprised me with its flexibiliy and power. It has convinced of the power of object oriented programming, particularly for working with databases.
I have been trying to come to grips with Curses, a library for character based interface development for some time. Finally, I have arrived at a point where I feel fairly comfortable with it.
So this is an investigation into these technologies : PostgreSQL, C++ and Curses.
Obtaining and installing PostgreSQL
First, PostgreSQL. Though PostgreSQL is one of the packages distributed on the RedHat Linux 6.1 CD I have, an early attempt to access data with the original distribution had such uneven results that I decided to download the latest version from the net. The URL is www.PostgreSQL.org. The version I downloaded was v7.0.2-2.
Basically, I downloaded a bunch of RPMs from the site. The ones that I really needed to install were
- PostgreSQL-7.0.2-2
- PostgreSQL-server-7.0.2-2
- PostgreSQL-devel-7.0.2-2
The 'devel' package was needed to get access to a header file, libpq-fe.h, and some contributed code.
I don't believe in re-inventing the wheel needlessly. So I basically tried out the code and modified it a bit to my purposes.
But the first thing I needed to do was, of course, read the README file. Then I set up a user called 'postgres' and a group called 'postgres'. Then, as 'root' I installed the RPMs. There is also a bunch of stuff that one should do logged as the user 'postgres'. This is fairly well detailed in the README file, so I am not going to repeat it here. It's basically about setting up things so you can run some tests that will ensure that everything is working. This is a really good idea, so I took my time and want through the steps carefully. It doesn't take much time and gave me a measure of confidence that everything was working properly.
One of the jobs of the 'Postgres' user is to create new users that can access PostgreSQL databases. So I ran 'createuser' and added myself as a PostgreSQL user. I also gave myself the permissions to create PostgreSQL databases, since I expected to be creating quite a few test databases.
That done, the most important task left was to setup PostgreSQL to run as a daemon (or in Microsoft parlance, a service). Basically, there's a tool called strangely enough, 'ntsysv'. This pops up a list of available daemons. I just marked with an asterick the line with PostgreSQL. That ensures that on the next reboot, PostgreSQL would be started.
Being too lazy to reboot, I ran the following command which does the same thing :-
/etc/rc.d/init.d/postgresql start
It comes back with some encouraging messages about starting the database. Finally, I could login under my own id and start running some SQL commands.
PostgreSQL provides an interpreter called 'psql'. This is a weird little tool with some amazing abilities. For one thing, it maintains a history of all commands previously used. It can save these to a file, so a simple way of making a script is to try out all the commands and then, save the history to a file. A little judicious editing and presto, instant script.
Psql also comes with on-line help, accessed with \? and \h.
- \? shows commands specific to the interpreter.
- \h shows help with basic SQL syntax.
There is also plenty more help in HTML format under /usr/doc/postgresql. I haven't finished reading it all yet.
Just entering 'psql' at the command prompt drops you to a # prompt. Typing SQL commands terminated with a semi-colon executes the command. Or returns an error message, if something went wrong. The usual range of 'readline' commands is available inside this interpreter. This is a blessing for me, after having to use MS-brain dead ISQL/W at work all day.
The basic 'create table', 'insert into', 'delete from' and 'select * from' commands all work exactly as one would expect. There is a full range of SQL functions and PostgreSQL provides a whole bunch of extra functions as well.
I haven't explored stuff like stored procedures, triggers, etc. simply because I have been concentrating on the Curses interface most of the time. But all that stuff is in there. Explore to your heart's content.
To be continued......(Next: C code to access PostgreSQL databases)
Contact the creator of this page
Any questions, comments, additional information about this page are welcome. Please contact us at: ttcs@opus.co.tt