如果我正确理解了这个问题,那么听起来您在SQL::Abstract之后。首先,我们创建一个SQL::Abstract
对象:
use SQL::Abstract;
my $sql = SQL::Abstract->new;
现在,作为示例,我们将使用它将一些数据插入到表中:
my %record = (
FirstName => 'Buffy',
LastName => 'Summers',
Address => '1630 Revello Drive',
City => 'Sunnydale',
State => 'California',
Occupation => 'Student',
Health => 'Alive',
);
my ($stmt, @bind) = $sql->insert(’staff’,\%record);
这导致:
$stmt = "INSERT INTO staff
(FirstName, LastName, Address, City,
State, Occupation, Health)
VALUES (?, ?, ?, ?, ?, ?, ?)";
@bind = ('Buffy','Summers','1630 Revello Drive',
'Sunnydale',’California','Student','Alive');
这样做的好处是我们可以将它直接传递给 DBI:
$dbh->do($stmt, undef, @bind);
当然,您希望更新记录,而不仅仅是插入它们。幸运的是,这也很容易:
my $table = 'People';
my %new_fields = (
Occupation => 'Slayer',
Health => 'Dead',
);
my %where = (
FirstName => 'Buffy',
LastName => 'Summers',
);
my ($stmt, @bind) = $sql->update($table, \%new_fields, \%where);
$dbh->do($stmt, undef, @bind);
这会产生:
$stmt = 'UPDATE People SET Health = ?, Occupation = ?
WHERE ( FirstName = ? AND LastName = ? )';
@bind = ('Dead', 'Slayer', 'Buffy', 'Summers');
如果您想了解更多信息SQL::Abstract
,我建议您查看其CPAN 页面。Perl Training Australia的Database Programming with Perl手册中还有一章,可从我们的课程笔记页面免费获得。
祝一切顺利,
保罗
免责声明:我是 Perl Training Australia 的常务董事,因此认为我们的课程笔记非常好。