***** infoCopter.com/perl *****

use DBI;



[ DBI Sample code ]

Usage

my $dbh = DBI->connect(
        "dbname=YOUR_DB_NAME",
        $config{'dbuser'}, '', 'Pg',
        { PrintError => 0,
          AutoCommit => 0
        }
) or Debug $DBI::errstr;


Sample

=========================================================================
my $dbh = DBI->connect( "dbname=auctionline", $config{'dbuser'}, '', 'Pg') or Debug $DBI::errstr;

my @db_fields = qw (items.id  description  configuration  location
        price  increment  count  sale_type  currency
        auction_enddate  status  subcategory  batchcode);

my %results = ();

my $stmt = qq~
SELECT items.id, description, configuration, location, price, increment, count, sale_type, currency,
auction_enddate, status, subcategory, batchcode FROM items WHERE subcategory = 101 AND status = 1;
~;

my $sth = $dbh->prepare($stmt) or Debug $DBI::errstr;
   $sth->execute() or Debug $DBI::errstr;

   $sth->bind_columns(map { \$results{$_} } @db_fields);

while ($sth->fetch()) {
        print qq~
        $results{'items.id'}
        $results{'description'}
        ~;
}

print q~
#------------ selected columns were -------------#
~;
my @selected_cols = @{$sth->{NAME}};
Debug "- $_" foreach @selected_cols;

$sth->finish();
$dbh->disconnect();
=========================================================================

Result

[root@reto alshop]# perl test.cgi

        265
        HP-LASERJET EX PLUS 3 / J2594A

        264
        HP-JETDIRECT EX PLUS 3 / J2594A

#------------ selected columns were -------------#
- id
- description
- configuration
- location
- price
- increment
- count
- sale_type
- currency
- auction_enddate
- status
- subcategory
- batchcode
NOTICE:  ROLLBACK: no transaction in progress


DBI->connect using 'old-style' syntax is deprecated and will be an error in future versions at yourscript.pl line number

Reason:

The $data_source value must begin with "dbi:driver_name:". The driver_name specifies the driver that will be used to make the connection. (Letter case is significant.)

DBI.pm:

sub connect {
    my $class = shift;
    my ($dsn, $user, $pass, $attr, $old_driver) = my @orig_args = @_;
    my $driver;

    if ($attr and !ref($attr)) { # switch $old_driver<->$attr if called in old style
        Carp::carp("DBI->connect using 'old-style' syntax is deprecated and will be an error in future versions");
        ($old_driver, $attr) = ($attr, $old_driver);
    }
    __snip__

New style:

DBI->connect('dbi:driver:...', $user, $passwd);
$dbh = DBI->connect(
        "dbi:Pg:dbname=your_dbname;host=localhost" , 
        $DATABASE{'user'}, $DATABASE{'pass'},
        { PrintError => 0,
          AutoCommit => 1
        }
);

Old style:

$dbh = DBI->connect(
        "dbname=$DATABASE{'db'};host=$DATABASE{'host'}" ,
        $DATABASE{'user'}, $DATABASE{'pass'}, $DATABASE{'driver'},
        { PrintError => 0,
          AutoCommit => 1
        }
);
© reto :)