我試圖運行使用PostgreSQL使用Perl
use DBI;
$database = "postgres";
$user = "postgres";
$password = "admin";
my $dbh = DBI->connect( "dbi:Pg:dbname=$database"
, $user
, $password
)
or die "Can't Connect to database: $DBI::errstr\n";
# get data from the URL string
my $firstname = "haroon";
my $lastname ="ash";
my $age = 24;
# insert the data into the database
my $query = "INSERT INTO people (firstname, lastname, age)
VALUES ('$firstname','$lastname', '$age')";
$dbh->do($query);
# get the ID of the inserted person
$query = "SELECT MAX(id) FROM people";
my $sth = $dbh->prepare($query);
my $rv =$sth->execute;
if($rv < 0){
print $DBI::errstr;
}
else {
my $row = $sth->fetchrow_hashref;
my $person_id = $row->{'max'};
print $firstname, $lastname
. "was successfully inserted at position "
. $person_id;
}
我想打印的我已經進入了最新的人物ID用Perl一個簡單的數據庫連接擷取來自數據庫的一個單列。但my $person_id = $row->{'max'}
似乎給我正確的答案,而不是my $person_id = $row->{'id'};
。我不明白這是爲什麼。
看看['爲postgresql' mysql_insert_id替代(HTTP://計算器.com/q/55956/1733163) – Miller