Perl DBI & DBD::pg inside the latest SXDE 01/08

The new released SXDE (01/08) bundles Perl DBI and Perl DBD::Pg along with many other web-stack components. This piece of integration I volunteered to do with a selfish personal goal of contributing more to the OpenSolaris codebase. It has been a great experience of following the entire Solaris integration process for the first time. With SXDE 01/08, the 2 components are out there for people to use.

DBI is a generic database Interface for Perl applications and DBD::Pg is the PostgreSQL specific database driver  which implements the DBI interface. Both these perl modules are integrated into latest Solaris. These components are ready to use once the OS is installed and no other configuration is required.

A simple usecase is as below,

 
First, Make sure that PG8.2 Service is running, else you need to enable the postgresql:version_82 service to start the PG postmaster.

bash-3.2$ svcs -a | grep postgres
disabled        6:03:01 svc:/application/database/postgresql:version_81
online          6:03:16 svc:/application/database/postgresql:version_82

Create a small database that we will try to access through a perl application using DBD::Pg.

bash-3.2$ /usr/postgres/8.2/bin/createdb testdb
CREATE DATABASE
bash-3.2$ /usr/postgres/8.2/bin/psql testdb
Welcome to psql 8.2.5, the PostgreSQL interactive terminal.

Type:  \\copyright for distribution terms
       \\h for help with SQL commands
       \\? for help with psql commands
       \\g or terminate with semicolon to execute query
       \\q to quit

testdb=# create table t (id int);
CREATE TABLE
testdb=# insert into t values (1);
INSERT 0 1
testdb=# insert into t values (2);
INSERT 0 1
testdb=# select \* from t;
 id
----
  1
  2
(2 rows)

testdb=# \\q

Write the perl app that will talk to this database. Here, we simply do a select on the table created above.

bash-3.2$ cat test.pm
use DBI;

$dbh = DBI->connect ( "dbi:Pg:dbname=testdb","","" ) ;
if ( !defined $dbh ) {
        die "Cannot connect to database!\\n";
}

print "Success connecting!\\n";

$sth = $dbh->prepare ( " SELECT t.id from t" ) ;
if ( !defined $sth ) {
        die "Could not prepare statement\\n";
}
$sth->execute;
while ( @row = $sth->fetchrow() ) {
        print @row,"\\n";
}

$sth->finish;
$dbh->disconnect;

Now, run the perl module to see the result. 

bash-3.2$ which perl
/usr/bin/perl
bash-3.2$ perl test.pm
Success connecting!
1
2
bash-3.2$

With DBI already in Solaris now, other drivers will also follow soon.

So, Go ahead and use them and Do not forget to file bugs if any, here.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

user13377336

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Blogroll

No bookmarks in folder