而不是“?”的集合使用数组

时间:2016-11-17 14:12:40

标签: sql perl dbi

如何在不使用大量字符的情况下发出请求,并使用数组代替它们?

在我的数组@rowCASHIERGRPs内,如“0001,0020,0027,0700”;

我试过了,但这不起作用。

首先:

use strict;
use warnings;

use DBIx::Simple;

my $dbfile='DPTDAT.db';
my $db = DBI->connect("dbi:SQLite:$dbfile", "", "",

my $result = $db->query(
'select b.ID,CASHNUM,f1,f2,f3,f4,f5,sum(f6),sum(f7),sum(f8) 
from CKRDATA as a 
inner join CKRDATCASHIERNAME as b 
where a.ID =b.ID and b.CASHNUM =? 
group by f1,b.ID order by b.ID,f1',
@rowCASHIERGRPs);

第二

use strict;
use warnings;

use DBI;

my $dbfile='DPTDAT.db';
my $db = DBI->connect("dbi:SQLite:$dbfile", "", "", {RaiseError => 1, AutoCommit => 1});

my $sqlt = "select b.ID,CASHNUM,f1,f2,f3,f4,f5,sum(f6),sum(f7),sum(f8) 
from CKRDATA as a
inner join CKRDATCASHIERNAME as b 
where a.ID = b.ID and b.CASHNUM IN (" .
"join(', ', ('?') x @rowCASHIERGRPs)" .
"group by f1, b.ID order by b.ID, f1";

my $dbqueryAllDataCKR = $db->prepare($sqlt);

$dbqueryAllDataCKR->execute(@rowCASHIERGRPs);

我想获得的示例数据:

"001""0002"":0000""XXXXXXXX"":00"":0000"":00""0""0""0.0"
"001""0002"":0001""MMMMMMMM"":11"":0000"":00""0""0""0.0"
"001""0002"":0002""YYYYYYYY"":26"":0000"":00""0""0""0.0"
"001""0002"":0009""zzzzzzzz"":01"":0000"":00""0""0""0.0"
"001""0002"":0101""VVVVVVVV"":17"":0086"":00""145""3432""35740099.0"

1 个答案:

答案 0 :(得分:1)

你的第一个例子不起作用,因为你没有终止connect陈述

my $db = DBI->connect("dbi:SQLite:$dbfile", "", "",

并且因为b.CASHNUM = ?只能比较一个值,但是您传递的是一整组值

你的第二个例子没有用,因为你有

"join(', ', ('?') x @rowCASHIERGRPs)" .

在双引号内,因此它将复制该表达式,而不是从数组值中构建逗号分隔列表

这可能对您有用,但您还没有显示任何数据,因此我无法对其进行测试

use strict;
use warnings 'all';

use DBI;

my $dbfile = 'DPTDAT.db';
my $db = DBI->connect("dbi:SQLite:$dbfile", "", "", { RaiseError => 1, AutoCommit => 1 });

my @row_cashier_grps;

my $placeholders = join(', ', ('?') x @row_cashier_grps);

my $sqlt = <<END_SQL;
SELECT cashier.ID, cashnum, f1, f2, f3, f4, f5, SUM(f6), SUM(f7), SUM(f8) 
FROM ckrdata AS data
INNER JOIN ckrdatcashiername AS cashier 
WHERE data.ID = cashier.ID
AND cashier.cashnum IN ($placeholders)
GROUP BY f1, cashier.ID
ORDER BY cashier.ID, F1
END_SQL

my $sth = $db->prepare($sqlt);

$sth->execute(@row_cashier_grps);