为什么 DBI "execute" 方法的 return 值得到 0E0?
Why am I getting 0E0 for the return value of the DBI "execute" method?
我编写了一个示例 Perl 程序来从数据库中删除数据 table。
这是我写的代码,
use DBI;
my $dbh = DBI->connect("DBI:Pg:host=192.168.12.23;port=5432;", "adhi");
if ( $dbh ) {
print "Connected successfully\n";
my $exe = $dbh->prepare("delete from perl_test.test");
my $res = $exe->execute();
if ( $res ) {
print "deleted the table successfully of rows: $res\n";
}
}
如果我执行了上面的操作,它应该打印成功的信息,然后是删除的行数。
如果 table 为空,则打印 0E0
而不是 0
。
我不知道它是如何返回这样的值的?
有人可以向我解释一下它是如何工作的吗?
这样做是为了测试操作是否成功。原因是 '0E0'
(作为字符串)是真值,但 0
在 Perl 中是假值。因此:
可以通过测试if
中的return值判断操作是否成功(true表示成功),
但您也可以使用 return 值作为数字来了解删除行的确切数量,因为 0E0 用作数字时计算结果为 0。
如果您只需要删除的行数,可以使用$res + 0
或$res * 1
。但是只有在你测试过后才能运行成功。
我编写了一个示例 Perl 程序来从数据库中删除数据 table。
这是我写的代码,
use DBI;
my $dbh = DBI->connect("DBI:Pg:host=192.168.12.23;port=5432;", "adhi");
if ( $dbh ) {
print "Connected successfully\n";
my $exe = $dbh->prepare("delete from perl_test.test");
my $res = $exe->execute();
if ( $res ) {
print "deleted the table successfully of rows: $res\n";
}
}
如果我执行了上面的操作,它应该打印成功的信息,然后是删除的行数。
如果 table 为空,则打印 0E0
而不是 0
。
我不知道它是如何返回这样的值的?
有人可以向我解释一下它是如何工作的吗?
这样做是为了测试操作是否成功。原因是 '0E0'
(作为字符串)是真值,但 0
在 Perl 中是假值。因此:
可以通过测试
if
中的return值判断操作是否成功(true表示成功),但您也可以使用 return 值作为数字来了解删除行的确切数量,因为 0E0 用作数字时计算结果为 0。
如果您只需要删除的行数,可以使用$res + 0
或$res * 1
。但是只有在你测试过后才能运行成功。