5
3

More than 5 years have passed since last update.

perlのDBIの、executeの戻り値

Posted at

perlのDBIの、executeの戻り値について。

以下のようなときの話。

    # あらかじめ接続した$dbhがあるとして。
    my $sth = $dbh->prepare('実行したいクエリ');
    my $rv = $sth->execute();

    if (!defined($rv)) {
        printf("Failed to query.\n");
        printf("%s\n", $sth->errstr);
        return;
    }

戻り値 意味
undef クエリの失敗
-1 影響があった(返ってきた)行が不明
0E0 影響があった(返ってきた)行が0
数値 影響があった(返ってきた)行数

影響があった行がない、または返ってきた行がない場合、0E0という文字列が返ってくる。

以下のように判断されるので、うまいこと使い分けることも、、、あるのかな?

a.pl
#! /usr/bin/perl -w

use strict;
use warnings;

my $a = '0E0';

printf("%s is %s\n", '$a', $a);

if ($a) {
    print('if ($a) ... true' . "\n");
} else {
    print('if ($a) ... false' . "\n");
}


if ($a > 0) {
    print('if ($a > 0) ... true' . "\n" );
} else {
    print('if ($a > 0) ... false' . "\n");
}

[root@localhost ~]# ./a.pl
$a is 0E0
if ($a) ... true
if ($a > 0) ... false
[root@localhost ~]#

-1が返ってくるパターンが良く分からない・・・。
クエリ自体は成功している、と考えていいのやら。

5
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3