Perl
事前準備
- 安裝 Perl DBI, 及 PostgreSQL driver module DBD:Pg
範例
初始 database handlermy $dbdriver = 'Pg'; my $dbname = 'myDB'; my $dbhost = 'localhost'; my $dbport = '5432'; my $dburl = "dbi:$dbdriver:dbname=$dbname;host=$dbhost;port=$dbport"; my $dbh = DBI->connect($dburl, $dbuser, $dbpass) or die "Can't connect to $dbdriver: $DBI::errstr ($DBI::err)\n";
執行 SQLmy $sql = "select * fom myTable where last_updated > '2009-01-01'"; my $sth = $dbh->prepare($sql); $sth->execute;
可取得 results 列數my $rows = $sth->rows;
對每一個 row 進行處理while ($rrow=$sth->fetchrow_hashref()) { my ($field1,$field2) = ($rrow->{field1_name}, $rrow->{field2_name}); .... }
|