我根據某些條件(例如操作類型或某個值的存在)通過將字段添加到INSERT
語句來構建查詢。但後來我有不同的DBI與不同的參數列表分支execute
S,這樣的:Perl DBI使用數組而不是標量列表執行
if ($x) {$extraFields .= ' , X'; $extraValues= ',? '}
if ($y) {$extraFields .= ' , Y, Z'; $extraValues= ',?, ? '}
my $theBasicQuery = "INSERT INTO sometable (A, B, $extraFields) VALUES (?, ? $extraValues)";
$sth = $dbh->prepare($theBasicQuery) or error
# but I dont want to have to do this if for execute
if ($x) {$sth->execute(1,2,99);}
if ($y) {$sth->execute(1,2,88, 77);}
我寧願做這樣的事情:
{$sth->execute($anArrayWithDifferentParams);}
這可能嗎?或者有另一種方法可以做類似的事情嗎?
爲什麼不將字段名稱存儲在一個數組中,然後在形成查詢字符串時加入數組? – 2013-02-27 20:58:20