I have this Perl script:
use v5.12;
use strict;
use warnings;
use DBI;
my $dsn = "DBI:mysql:host=localhost;database=testitt";
my $dbh = DBI->connect($dsn,"root","")
or die "No db connectin: $!";
say "Connected to MySQL.";
my $source_dir = "C:/Users/ST/Desktop/";
opendir my $dirh, $source_dir or die "Unable to open directory: $!";
my @files = grep /\.csv$/i, readdir $dirh;
closedir $dirh;
die "No files found in $source_dir" unless @files;
say 'Importing data from:';
say for @files;
my $load = $dbh->prepare("LOAD DATA INFILE ? INTO TABLE tablea Character Set utf8 FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\n' IGNORE 1 LINES (id, normalized_count)")
or die "Prepare failed: " . $dbh->errstr();
for my $file (@files) {
say "Processing $source_dir/$file";
open my $fh, '<', "$source_dir/$file"
or die "Unable to open $source_dir/$file: $!\n";
$load->execute($file)
or die "Execute failed: " . $dbh->errstr();
}
print "Jobs done.\n";
It gives me error at the $load->execute($file) because it's unable to find the file, but I've already loaded everything on @file early in the code.
EDIT: Thanks to matt suggestion my for now is cleaner but i can't figure out a way to put the current file into the query (the token), below an attempt but gives error:
for my $file (@files) {
my $statement = $dbh->prepare("LOAD DATA INFILE C:\\Users\\ST\\Desktop\\"$file"INTO TABLE rnaseq Character Set utf8 FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\n' IGNORE 1 LINES (id,normalized_count)");
$statement->execute()