在这种情况下,将 undef 传递给 DBI 的 `do` 方法的目的是什么?

问题描述

我不明白 undef 在这代码中做了什么:

$dbh->do (qq {
            INSERT INTO todo SET t = Now(),status = 'open',content = ?
        },undef,$content);

谁能解释一下?我想我了解整个代码,但不了解它的来源。

use warnings;
use strict;
use lib q(/data/TEST/perl/lib);
use CGI qw(:standard);
use WebDB;

sub insert_item {
    my $content = shift;
    my $dbh;
    $content =~ s/^\s+//;
    $content =~ s/^\s+$//;
    if ($content ne "") {
        $dbh = WebDB::connect();
        $dbh->do (qq {
            INSERT INTO todo SET t = Now(),$content);
        $dbh->disconnect();
    }
}

sub display_entry_form {
    print start_form(-action=> url()),"To-do item:",br (),textarea ( -name => "content",-value => "",-override => 1,-rows =>3,-columns => 80),submit(-name=> "choice",-value => "Submit"),end_form();
}

print header(),start_html(-title=>"To-Do List",-bgcolor => "white"),h2("To-Do List");

my $choice = lc(param ("choice"));

if ($choice eq "") {
    display_entry_form();
} elsif ( $choice eq "submit" ) {
    insert_item(param("content"));
    display_entry_form();
} else {
    print p ("Logic error,unkNown choice: $choice");
}

解决方法

do() 方法接受 3 个参数:查询、查询属性和绑定数据。您示例中的 undef 表示没有要应用的属性。

"do()" in DBI on CPAN

$rows = $dbh->do($statement)           or die $dbh->errstr;
$rows = $dbh->do($statement,\%attr)   or die $dbh->errstr;
$rows = $dbh->do($statement,\%attr,@bind_values) or die ...