简体   繁体   中英

inserting date in MySQL using Perl

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?

It seems UnixDate() is returning undef 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM