一些SQL客戶做提供便利,讓您直接從文件創建查詢或數據線動態創建的查詢。不過,我相信更好的方法是爲此編寫一個腳本。
根據你最近給喜歡this one其他問題,那麼也許在Perl下面的解決方案可以幫助你:
use 5.012;
use warnings;
use SQL::Abstract;
my $sql = SQL::Abstract->new;
my @cols = qw/ idA idB stringValue /;
my $where = build_sql_where_from_file('list.txt', @cols[0,1]);
my ($query, @bind) = $sql->select(
'yourTable',
\@cols,
$where,
);
sub build_sql_where_from_file {
my $file = shift;
my @build;
open my $fh, '<', $file or die $!;
for my $line (<$fh>) {
chomp $line;
my @fields = split//, $line;
push @build, {
-and => [
map { $_ => shift @fields } @_,
]
};
}
return \@build;
}
如果我現在要做以下使用例如list.txt
...
say $query;
say join ":", @bind;
然後我得到:
SELECT idA, idB, stringValue FROM yourTable WHERE (((idA = ? AND idB = ?) OR (idA = ? AND idB = ?) OR (idA = ? AND idB = ?)))
xyz:2:abc:5:abc:6
這正是我n然後直接插入DBI
查詢。
希望有所幫助。
/I3az/
但我可以使用一個文件來做它的每一行嗎? – 2010-09-15 09:18:18
不需要。您需要編寫一個腳本來幫助您。 – halfdan 2010-09-15 09:19:03