Leser: 1
10 Einträge, 1 Seite |
1
2
3
4
..
my $dbh = DBI->connect("dbi:Pg:dbname=Raum;host=127.0.0.1;Port=5342", "perl_db", "****", { RaiseError => 0, AutoCommit => 0 }) or croak $DBI::errstr;
..
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
1. Die SQL-Anweisung vorbereiten.
2. Die SQL-Anweisung ausfuehren.
3. Die Ergebnisse abrufen.
4. Die SQL-Transaktion beenden.
# 1. prepare statement handle $sth
$cmd = 'SELECT * FROM table WHERE id = ? AND date > ?'; my $sth = $dbh->prepare_cached($cmd)
or croak "SQL prepare failed '$cmd': " . $dbh->errstr;
# 2. execute statement (via its handle)
my $rc = $sth->execute(parameter1, parameter2, ...);
# execute() has as many arguments as the prepared statement ?'s has unless(defined $rc) {
croak "SQL command failed: " . $dbh->errstr; } else {
# $rc rows had been effected, e.g. by UPDATE commands }
# 3. fetch results [if any] (again via its handle) while(my $result = $sth->fetchrow_arrayref) {
# ARRAY ref $result has one entry per column
# $result->[0] - first column, etc.
}
croak "Failed to get results from SQL command: "
. $dbh->errstr if $sth->err;
- -or-
while(my $result = $sth->fetchrow_hashref) {
# HASH ref $result has coumn_name => value pairs
# $result->{name1}, $name->{name2}, ...
}
croak "Failed to get results from SQL command: "
. $dbh->errstr if $sth->err;
# 4. finish DB transaction (via DB handle)
$dbh->commit() or die "Failed to save modified data: " . $dbh->errstr;
- -or-
$dbh->rollback() or die "Failed to discard modified data: " . $dbh->errstr;
my $aref = $dbh->selectall_arrayref(qq{SELECT ...},undef,$param1,...);
$dbh->do(...);
$dbh->do(...);
$dbh->do(CREATE TABLE t_kontakte);
my $aref = $dbh->selectall_arrayref(qq{SELECT ...},undef,$param1,...);
qq{SELECT blafasel FROM blubb}
10 Einträge, 1 Seite |