perl DBI recipes column array



下面是我的sql查询输出

Company Col1    Col2    Col3
Comp1   1       2       3
Comp2   4       5       6
Comp3   7       8       9

用于连接和检索结果的 Perl 过程

my $query1= qq(select * from database_table);
my $result1 = $dbh->selectall_arrayref($query1, {Slice => {}}); 
  my %result1 =
        map { shift @$_, [ @$_ ]}
        @{$dbh->selectall_arrayref($query1)};
    my @json_output = map { encode_json( { 'name' => $_, 'data'=> $result1{$_} } )
                  } sort keys %result1 ;
    print Dumper %result1;
    [{"name":"Comp1","data":[1,2,3]}, {"name":"Comp2","data":[4,5,6]}, {"name":"Comp3","data":[7,8,9]}]

我一直在阅读 http://www.perlmonks.org/?node_id=284436#fetching 但我不知道如何将列值存储为数组元素。(如下图所示(

    [{"name":"Col1","data":[1,4,7]}, {"name":"col2","data":[2,5,8]}, {"name":"col3","data":[3,6,9]}]

此外,默认情况下,值字段在 json 中显示为"字符串",关于如何将它们转换为数字的任何建议?

这是

怎么回事

use strict;
use warnings;
use DBI;
use DBD::mysql;    
use JSON;    
use Data::Dumper;
my $dbh = DBI->connect('DBI:mysql:dbnew:localhost');    
my $query1=qq(select * from database_table);    
my $sth=$dbh->prepare($query1);    
$sth->execute;
my @col_names=@{$sth->{NAME}};
my %result1;    
for(my $i=0;$i<3;$i++)
{
my @res = map { $_->[$i]} @{$dbh->selectall_arrayref($query1)};
@res=map {$_ * 1} @res;
$result1{shift @col_names}=@res;
}
my @json_output = map {encode_json( { 'name' => $_ , 'data'=> $result1{$_} } )}  sort keys %result1;
print @json_output;

假设我有你显示的表格,但称为"fm"。

use DBI;
use strict;
use warnings;
use Data::Dumper;
use JSON::XS;
my $h = DBI->connect('dbi:ODBC:xxx', 'xxx', 'xxx');
my $r = $h->selectall_arrayref(q/select company, col1, col2, col3 from fm/);
print Dumper($r);
my @to_encode;
foreach my $row (@$r) {
    my $hash;
    $hash->{name} = shift @$row;
    $hash->{data} = $row;
    push @to_encode, $hash;
}
my $js = encode_json(@to_encode);
print Dumper($js);

输出:

$VAR1 = [
          [
            'comp1',
            '1',
            '2',
            '3'
          ],
          [
            'comp2',
            '4',
            '5',
            '6'
          ],
          [
            'comp3',
            '7',
            '8',
            '9'
          ]
        ];
$VAR1 = '[{"name":"comp1","data":["1","2","3"]},{"name":"comp2","data":["4","5","6"]},{"name":"comp3","data":["7","8","9"]}]';

编辑:重新阅读您的示例,我认为这确实是您想要的:

use DBI;
use strict;
use warnings;
use Data::Dumper;
use JSON::XS;
my $h = DBI->connect('dbi:ODBC:baugi', 'sa', 'easysoft');
my $s = $h->prepare(q/select col1, col2, col3 from fm/);
$s->execute;
my $cols = $s->{NAME};
my @data;
for (my $n = 0; $n < scalar(@$cols); $n++)  {
    push @data, {name => $cols->[$n], data => []};
}
while (my @row = $s->fetchrow) {
    for (my $n = 0; $n < scalar(@$cols); $n++) {
        push @{$data[$n]->{data}}, shift @row;
    }
}
my $js = encode_json(@data);
print Dumper($js);
$VAR1 = '[{"name":"col1","data":["1","4","7"]},{"name":"col2","data":["2","5","8"]},{"name":"col3","data":["3","6","9"]}]';

可能还有更优雅的方法可以做到这一点,并用更好的SQL简化Perl工作,但现在还为时过早,我还没有喝第一杯咖啡。

正如您所指出的,这些数字看起来像编码的 JSON 中的字符串。这是因为您的 JSON 模块(无论如何都是 JSON::XS(在标量上使用类似 sv_POK 的东西来尝试猜测它们是数字还是字符串,并且大多数 DBD 模块将所有列绑定为字符串并使用 sv_setpv 设置返回的标量。这很烦人,但在拨打 OR 之前,您需要为每个号码添加 0 encode_json:

碰巧我碰巧正在更改 DBD::ODBC,因此它将整数绑定为整数 - 请参阅 Perl DBD::ODBC 中列绑定的重大更改

使用 DBD::Oracle 您可以绑定列SQL_INTEGER添加 DiscardString 属性,例如,

$s->prepare(q/select company,col1,col2,col3 from mytable);
$s->execute;
$s->bind_col(2, undef, {TYPE => SQL_INTEGER, DiscardString => 1});
# repeat for col2 and col3
$r = $s->fetchall_arrayref

我相信其他一些 DBD 已经将整数绑定为整数 - 可能是 DBD::P g。

相关内容

  • 没有找到相关文章

最新更新