【问题标题】:What is the purpose of passing undef to DBI's `do` method in this context?在这种情况下将undef传递给dbi的方法的目的是什么?
【发布时间】:2021-04-28 20:47:10
【问题描述】:

我不明白undef 在这个 sn-p 中做了什么:

$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(), status = 'open', content = ?
        }, undef, $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),
    br (),
    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");
}

【问题讨论】:

  • 相当于传递一个空的hashref。有关更多信息,请参阅 DBI 文档。

标签: perl dbi


【解决方案1】:

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 ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-26
    • 2013-04-23
    • 2021-12-27
    • 2016-06-02
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多