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 31 32 33 34 35 36 37 38 39 40 41 42
| #!/com/ASTRIDFS2/webmsd/site_pay/tools/appli_search/ActivePerl-5.10/bin/perl
use warnings;
use strict;
use lib (
"/com/ASTRIDFS2/webmsd/site_pay/tools/appli_search/ActivePerl-5.10/lib",
"/com/ASTRIDFS2/webmsd/site_pay/tools/appli_search/ActivePerl-5.10/site/lib"
);
use DBI;
# Connect to the database.
my $dbh = DBI->connect(
"DBI:mysql:database=platform;host=localhost",
"myadm05", "myadm05", { 'RaiseError' => 1 }
) or die "Can't connect to database: platform\n$DBI::errstr";;
# Drop table 'foo'. This may fail, if 'foo' doesn't exist.
# Thus we put an eval around it.
eval { $dbh->do("DROP TABLE foo") };
print "Dropping foo failed: $@\n" if $@;
# Create a new table 'foo'. This must not fail, thus we don't
# catch errors.
$dbh->do("CREATE TABLE foo (id INTEGER, name VARCHAR(20))");
# INSERT some data into 'foo'. We are using $dbh->quote() for
# quoting the name.
$dbh->do( "INSERT INTO foo VALUES (1, " . $dbh->quote("Tim") . ")" );
# Same thing, but using placeholders
$dbh->do( "INSERT INTO foo VALUES (?, ?)", undef, 2, "Jochen" );
# Now retrieve data from the table.
my $sth = $dbh->prepare("SELECT * FROM foo");
$sth->execute();
while ( my $ref = $sth->fetchrow_hashref() ) {
print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}\n";
}
$sth->finish();
# Disconnect from the database.
$dbh->disconnect(); |
Partager