× News Cego SysMT Croom Web Statistics Impressum
SysMT Logo
10   Using the DBD Driver
Back to TOC

In the perl programmers community, a very popular way to connect to a database system is using the database independent interface ( DBI ), which hides special database aspects and features from the programmer. Using this interfaces, requires a database dependent driver implementation ( DBD ) for each supported database. There is also available a DBD implemention for the Cego database system, which is described more detailed in the following section.

( The DBD-cego package can be downloaded as opensource from www.lemke-it.com )

10.1   DBD connection string

To connect to a running cego database daemon, several connection parameters must be set up correctly. The DBI connect method expects three parameters. First a connection string containing the driver class and driver class parameters. For the Cego DBD driver, this is a tableset, a hostname, a portnumber and the network protocol type ( serial or xml ). See the connection sample below for correct usage.

As second and third parameter, a valid username and password must be given. This must be a registered cego database user.

use DBI;
my $dbh = DBI->connect("dbi:Cego:tableset=TS1;hostname=geek;port=2200;protocol=xml", "lemke", "lemke")
                || die  "Database connection not made: $DBI::errstr";

The connect method returns a valid database handle or in case of any connection establish error, stores a error message in the DBI variable $DBI::errstr

10.2   Executing statements immediately

After a connection has been established successful, any queries can be processes using the db handle. For immediate processing, the DBI do method can be called. The method expects any valid static sql query. Normally, the do-method is used for any object creation

$dbh->do("create table tab1 (id int, name string(20));");

10.3   Executing prepared statements

In a more sophisticated way to process sql queries, prepared statements are used. Prepared statements are sql statements, containaining placeholders which later are bind to appropriate values. First, the statement is declared using the prepare method.

my $sth = $dbh->prepare("insert into tab1 values ( ? , ? );");

Any placeholders are represented by the ? sign. Now they can be bind to values. The first argument of the bind_param method indicates, which placeholder in order should be treated.

$sth->bind_param(1, 55, SQL_INTEGER);
$sth->bind_param(2, 'udo', SQL_VARCHAR );

After all placeholders are set up, the query can be executed

$sth->execute;

10.4   Fetching data

In case of any select queries, after executing the query, the result data must be fetched. The DBI fetchrow_array provides a way to do this.

my $sth = $dbh->prepare("select a, b from mytab;");

$sth->execute;

print "Query will return $sth->{NUM_OF_FIELDS} fields\n";
print "Field names: @{ $sth->{NAME} }\n";

while (($a, $b) = $sth->fetchrow_array) {
   print "a=$a b=$b\n";
}

The samples shown above give a small impression of how to use the DBD driver in a perl program. For a more detailed description of how to use DBI, please refer to the native DBI user documentation.