I am relatively new to Perl.I have been trying to insert data to database from a text file using a CGI script.I have written code for it and it’s working properly.but when i try to impose a limit on the data that is inserted using LIMIT keyword there is a problem.Please check where i am going wrong and what needs to be amended.Thanks for your advice.
here is the code
#!/usr/bin/perl
use warnings;
use strict;
use CGI ':standard';
use DBI;
if(param())
{
my @params=param();
my $limit=param('limit')||'';
my $dbh =DBI->connect("DBI:mysql:sample","root","");
my $var="LOAD DATA LOCAL INFILE 'C:/test.txt' INTO TABLE sample2 FIELDS TERMINATED BY '\n' WHERE LIMIT 0,$limit";
my $sth = $dbh->do($var) or die "prepare failed: " . $dbh->errstr();
print $sth ."Records inserted";
$sth->finish();
$dbh->disconnect();
print
header(),
start_html(
-title=>'Welcome',
-text=>'#520063'
),
#h1("Records have been displayed"),
end_html();
}
else
{
print
header(),
start_html('A Simple Form'),
h1('Please enter the limit '),
start_form(),
'Limit: ',
textfield(-name=>'limit'),
br(),
#'Phone Number: ',
#textfield(-name => 'number'),
#br(),
submit(),
end_form(),
end_html();
}
Without an error message I can’t be totally sure, but the problem is likely in your SQL.
There’s problems.
Basically that whole last line won’t work. In general, if a SQL command doesn’t work in a program try firing up the MySQL command line and debug the command there.
LOAD DATA INFILE does not have a way to limit how many lines it will pull in, this is an oft requested feature. To work around it I would suggest copying and truncating the file before feeding it to MySQL.