mysql windows Perl脚本在LINUX上运行

uujelgoq  于 2022-10-31  发布在  Mysql
关注(0)|答案(1)|浏览(175)

我试图让一个windows perl脚本在linux上运行,但它不工作,我想知道我犯了什么错误。
原来是在windows上运行,连接到本地mysql db,现在想转到linux上,在Linux上安装了docker,想连接到container中的mysql db,修改后的脚本不断报错
在Windows上的mysql版本是5.1
Linux上的mysql版本为8.0
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);
}

在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);    
}

这是执行后的错误代码

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
j9per5c4

j9per5c41#

您需要转义作为\n一部分的反斜杠,以发送实际的换行符,而不是n

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

相关问题