我试图将一个 txt 文件从 Perl 传输到 SQL Server,但我现在需要在 SQL 中创建一个数据库,它结合了数百个 txt 文件。是否有一种自动化的方式来保存手动传输所有 txt 文件?我将插入我对一个 txt 文件进行编码的尝试。我相信我需要使用 Bulk Insert 来实现这一点,但我不确定要使用什么代码。任何关于该问题的建议将不胜感激,如果您希望看到更多代码或我忘记提及的特定内容,请发表评论,我将进行编辑。谢谢你。
#!/usr/bin/perl -w
use strict;
use DBI;
use Carp;
# Connect to the data source and get a handle for that connection.
my $dbh = DBI->connect('dbi:ODBC:RegressionDBS') ||
confess "Connection Failed: $DBI::errstr";
# This query generates a result set with one record in it.
my $sql = "SELECT 1 AS test_col";
my $tablename = "RegressionTable";
open(FH, "newregression.txt") || die "cant open file";
my $Sqlh = $dbh->prepare("INSERT INTO RegressionTable (Ticker, TradeDate, HighPrice, LowPrice, TradePrice, TotalVolume, TotalValue) VALUES (?,?,?,?,?,?,?);") || confess $DBI::errstr;
while (my $line = <FH> )
{
$line =~/^(.*)(..)(..)$/;
my ($curTicker, $wTradeDate, $wHighPrice, $wLowPrice, $wTradePrice, $wTotalVolume, $wTotalValue);
$curTicker=$1
#$wTradeDate=
$wHighPrice=$2;
$wLowPrice=$1;
#$wTradePrice=$1;
#$wTotalVolume=$1;
#$wTotalValue=$4;
$Sqlh->execute($curTicker, $wTradeDate, $wHighPrice, $wLowPrice, $wTradePrice, $wTotalVolume, $wTotalValue) || confess $DBI::errstr;
}
$Sqlh->finish();
$dbh->disconnect();