首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >perl DBI配方列数组

perl DBI配方列数组
EN

Stack Overflow用户
提问于 2012-05-21 12:17:37
回答 2查看 2.9K关注 0票数 1

下面是我的sql查询输出

代码语言:javascript
运行
复制
Company Col1    Col2    Col3
Comp1   1       2       3
Comp2   4       5       6
Comp3   7       8       9

连接和检索结果的Perl过程

代码语言:javascript
运行
复制
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,但我不知道如何将列值存储为数组元素。(如下所示)

代码语言:javascript
运行
复制
    [{"name":"Col1","data":[1,4,7]}, {"name":"col2","data":[2,5,8]}, {"name":"col3","data":[3,6,9]}]

另外,值字段在json中默认为"string“,有什么建议如何将它们转换为数字吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-21 15:19:19

下面是它应该是怎样的

代码语言:javascript
运行
复制
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;
票数 1
EN

Stack Overflow用户

发布于 2012-05-21 15:47:29

假设我有你所展示的表格,但是叫做"fm“。

代码语言:javascript
运行
复制
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);

输出:

代码语言:javascript
运行
复制
$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"]}]';

编辑:重读你的例子,我认为这真的是你想要的:

代码语言:javascript
运行
复制
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 OR之前,您需要在每个数字上加上0:

我碰巧更改了DBD::ODBC,这样它就可以将整数绑定为整数--参见Major changes to column binding in Perl DBD::ODBC

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

代码语言:javascript
运行
复制
$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。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10679453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档