1

下面是我的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 中以“字符串”的形式出现,关于如何将它们转换为数字的任何建议?

4

2 回答 2

1

这就是它的样子

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;
于 2012-05-21T07:19:18.703 回答
1

假设我有您展示的表格,但称为“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 设置返回的标量。这很烦人,但您需要在调用 encode_json 之前为每个数字添加 0 或:

纯属偶然,我碰巧正在更​​改 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::Pg。

于 2012-05-21T07:47:28.533 回答