HomePractices Page 4 - Database Independent Development in C
An Example - Practices
The libdbi library provides a feature for C programmers that has long been missing. The interface is clean and the framework for adding new drivers is relatively straightforward.
This is a very simple utility that I created to query the user list for an application I've been working on. Most applications will be a little more complex.
#include <stdio.h> #include <dbi/dbi.h>
int main() {
dbi_conn db; dbi_result result;
unsigned long user_id; const char* name; const char* email; int drivercount;
drivercount = dbi_initialize("/usr/local/lib/dbd"); printf("Loaded %d drivers\n", drivercount); db = dbi_conn_new("mysql");
On my installation, providing that I called this userlist.c, my command line would look like this:
cc userlist.c -o userlist -I/usr/local/include -L/usr/local/lib -ldbi
As you can see, this program is blissfully short. It's so short in fact that it makes C a very good candidate for handling some of the routine daily reporting work that every IT department has to suffer through. Generate some HTML output instead of the plain text that I showed and you start to put out very nice looking nightly reports. In later articles I'll also discuss the use of libraries for generating PDF output. With that under your belt you'll have a reporting tool that can rival what commercial solutions can provide.