- or download this
use strict;
...
# Disconnect from the database
$dbh->disconnect();
- or download this
perldoc DBD::Oracle
- or download this
# Connect to the database
my $dbh = DBI->connect('DBI:mysql:my_database', 'my_username', 'my_pas
+sword')
or die "Couldn't open database: '$DBI::errstr'; stopped";
- or download this
my $sth = prepare(q{SELECT foo FROM bar WHERE baz='bap'})
or die "Couldn't prepare statement: $DBI::errstr; stopped";
- or download this
$sth->execute() or die "Couldn't execute statement: $DBI::errstr; stop
+ped";
- or download this
# Fetch each row and print it
while ( my ($field1, $field2, $field3) = $sth->fetchrow_array() ) {
print STDOUT "Field 1: $field1 Field 2: $field2 Field 3: $field
+3\n";
}
- or download this
# Fetch each row and print it-- fetchrow_arrayref
while ( my $fields = $sth->fetchrow_arrayref() ) {
print STDOUT "Field 1: $fields->[0] Field 2: $fields->[1] Field
+ 3: $fields->[2]\n";
}
- or download this
# Fetch each row and print it-- fetchrow_hashref
while ( my $field_hash = $sth->fetchrow_hashref() ) {
print STDOUT "Field 1: $field_hash->{'field1'} Field 2: $field_h
+ash->{'field2'} Field 3: $field_hash->{'field3'}\n";
}
- or download this
# A bad way of doing it
while (my $furniture_name = <STDIN>) {
...
my ($price) = $sth->fetchrow_array();
print STDOUT "Item: $furniture_name Price: $price\n";
}
- or download this
# A better way of doing it
my $sth = $dbh->prepare("SELECT price FROM furniture_prices WHERE furn
+iture_name=?")
...
my ($price) = $sth->fetchrow_array();
print STDOUT "Item: $furniture_name Price: $price\n";
}
- or download this
# One way of reading multiple rows
my $sth = $dbh->prepare("SELECT furniture_name, price FROM furniture_p
+rices WHERE furniture_type=?")
...
print STDOUT "Item: $furniture_name Price: $price\n";
}
}
- or download this
$sth->bind_col($column, \$scalar)
- or download this
# Faster way of reading multiple rows
my $sth = $dbh->prepare("SELECT furniture_name, price FROM furniture_p
+rices WHERE furniture_type=?")
...
print STDOUT "Item: $furniture_name Price: $price\n";
}
}