使用 Perl 在 MySQL 中插入日期
我有以下代码:
my $tdate = $fields[0];
print "$tdate\n";
my $trade_dt=UnixDate($tdate,"%Y-%m-%d");
# my $trade_dt = DATE(\"$tdate\");
print "$trade_dt\n";
my $ins_rec = "INSERT IGNORE INTO $tblname(\`trade_dt\`,\`symbol\`) va +lues (?,?);";
my $sth=$dbh->prepare($ins_rec);
$sth->execute($trade_dt,$symbol);
$sth->finish;
当我运行它时,我收到以下消息:
"10/14/2011" Use of uninitialized value $trade_dt in concatenation (.) or string at + Set.data.pl line 42, <FIN> line 1.
关于如何修复它的任何想法....它显然是从文件中正确读取日期,因为打印语句显示了 tdate 的正确日期这是从我拥有的 csv 文件中读取的,但转换为 trade_dt 不正确,是吗?
I have the following code:
my $tdate = $fields[0];
print "$tdate\n";
my $trade_dt=UnixDate($tdate,"%Y-%m-%d");
# my $trade_dt = DATE(\"$tdate\");
print "$trade_dt\n";
my $ins_rec = "INSERT IGNORE INTO $tblname(\`trade_dt\`,\`symbol\`) va +lues (?,?);";
my $sth=$dbh->prepare($ins_rec);
$sth->execute($trade_dt,$symbol);
$sth->finish;
and when I run it, I get the following message:
"10/14/2011" Use of uninitialized value $trade_dt in concatenation (.) or string at + Set.data.pl line 42, <FIN> line 1.
any thoughts on how I can fix it....it is obviously reading the date correctly from the file as the print statement shows the correct date for tdate which is reading from the csv file I have, but the conversion to trade_dt is not correct, is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎 UnixDate() 正在返回 undef 因为它无法解析您提供的日期。我怀疑问题是你的日期字符串被双引号包围,并且它们正在抛出解析器。去掉引号(使用诸如
$tdate =~ s/^"(.*)"$/$1/;
之类的东西),看看是否有帮助。It seems
UnixDate()
is returningundef
because it can't parse the date you're giving it. I suspect the problem is that your date string is surrounded by double quotes, and they're throwing the parser off. Get rid of the quotes (using something like$tdate =~ s/^"(.*)"$/$1/;
) and see if that helps.