windows Perl script run on LINUX

92 Views Asked by At

I'm trying to let a windows perl script run on linux, but it's not work, I want to know what mistake I made.

Originally, it was running on windows and connect to the local mysql db, but now I want to transfer it to linux. I installed docker on Linux and wanted to connect to the mysql db in container, but the revised script kept reporting an error

On Windows mysql version is 5.1
on Linux mysql version is 8.0

original script on windows

foreach my $file (@files) {
    $command = '-e "LOAD DATA LOCAL INFILE \''.$file.'\' REPLACE INTO TABLE test FIELDS TERMINATED BY \'|\' LINES TERMINATED BY \'\\r\\n\' IGNORE 1 LINES (@v001, @v002, @v003, @v004, @v005) SET `CA` = TRIM(@v001), `CB` = TRIM(@v002),`CD` = TRIM(@v003),`CE` = TRIM(@v004),`CF` = TRIM(@v005);"';
    system('mysql', '-hlocalhost', $user, $password, $command)  == 0 or err("ERROR:Failed to load file to test: $! \n");
    $nfile = "D:\\DONE\\".getmodifydate($file); #file last modify time
    move($file, $nfile);
}

On Linux

sub docker_mysql {
    $docker_mysql = "mysql", "-h$localhost", $database, $user, $password;
}

system(docker_mysql(), '-e', "DELETE FROM test WHERE 1") == 0 or 
    err("ERROR:Failed to delete record from test: $! ");
 
foreach my $file (@files) {
    $command = "LOAD DATA LOCAL INFILE \'$file'\' REPLACE INTO TABLE test FIELDS TERMINATED BY \'|\' LINES TERMINATED BY \'\\n\' IGNORE 1 LINES (\@v001, \@v002, \@v003, \@v004, \@v005) SET `CA` = TRIM(\@v001), `CB` = TRIM(\@v002),`CD` = TRIM(\@v003),`CE` = TRIM(\@v004),`CF` = TRIM(\@v005);";
    system(docker_mysql(), '-e', $command,)  == 0 or 
        err("ERROR:Failed to load file to test: $! \n");
    $nfile = "./DONE/".getmodifydate($file); #file last modify time
    move($file, $nfile);    
}

This is the error code after execution

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use 
near '|' LINES TERMINATED BY '
' IGNORE 1 LINES (@v001, @v002, @v003, @v004, @v005) SE' at line 1
1

There are 1 best solutions below

1
treuss On

You need to escape the backslash that is part of \n to send an actual line-feed, not an n.

    $command = "[...] LINES TERMINATED BY \'\n\' [...]";
                                            ^^ This should be \\n