連接到使用DBI
數據庫,準備查詢,執行查詢並獲取結果:
#!/usr/bin/env perl
use strict;
use warnings;
use DBI;
my %db_config = (
'database' => 'your_database_name',
'hostname' => 'your_hostname',
'port' => 'your_port',
'username' => 'your_username',
'password' => 'your_password',
);
my $dbh = DBI->connect(
"DBI:mysql:database=$db_config{database};host=$db_config{hostname};port=$db_config{port}",
$db_config{'username'}, $db_config{'password'},
) or die DBI->errstr();
my $sth = $dbh->prepare('SELECT todoid, todourl FROM todo')
or die DBI->errstr();
$sth->execute() or die DBI->errstr();
my %todo;
while (my $row = $sth->fetchrow_hashref()) {
$todo{ $row->{'todourl'} } = $row->{'todoid'};
}