gpt4 book ai didi

perl - 使用Perl的DBI,如何连接fetchrow_arrayref的结果?

转载 作者:行者123 更新时间:2023-12-03 02:10:34 27 4
gpt4 key购买 nike

我是 Perl 新手,我正在编写一个脚本来从数据库中获取一些行:

my @rows = $conn->fetchrow_array(1,2,3);

结果将是三行单列。

12345
56789
12376

我应该如何将它们连接在一起 12345,56789,56789

我试过了,

my $list = join ",", @rows. 

结果:数组(0x14f6de0),数组(0x1508a90),数组(0x15014c0)

通过 foreach 循环只需换行打印结果:

12345
56789
12376

我做错了什么?我对 fetchrow_array 的概念有误吗?

最佳答案

每一行都是对数组的引用(因为每一行可以包含多列)。像下面这样的东西应该可以工作。

#!/usr/bin/perl

use strict; use warnings;

my @rows = (
[ 12345 ],
[ 56789 ],
[ 12376 ],
);

my @vals = map @$_, @rows;

print join(',', @vals), "\n";

但是,您最好使用 selectcol_arrayref :

This utility method combines "prepare", "execute", and fetching one column from all the rows, into a single call. It returns a reference to an array containing the values of the first column from each row.

关于perl - 使用Perl的DBI,如何连接fetchrow_arrayref的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7996134/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com