我正在编写一个 Perl 脚本,以使用 OSX 中的 Automator 自动将 Excel 文档上传到 MySQL 数据库表中。我已经写了一个功能查询:
load data
local infile '/pathToFile/file.csv'
replace into table database.table
fields terminated by ','
enclosed by '"'
lines terminated by '\r'
ignore 1 lines;
问题是我的 Perl 脚本由于与\
角色冲突而无法运行。我是一个 Perl 新手,所以我不知道如何正确地转义这个字符以使查询工作。
我的 Perl 脚本:
#!/usr/bin/perl
# PERL MODULE
use Mysql;
# HTTP HEADER
print "Content-type: text/html \n\n";
# Set Variables
$host = "127.0.0.1";
$database = "database";
$tablename = "plugin_status";
$user = "user";
$pw = "password";
# PERL MySQL Connector
$connect = Mysql->connect($host, $database, $user, $pw);
# Run Query to update table
$myquery = "load data local infile '/pathToFile/file.csv' replace into table database.table fields terminated by ',' enclosed by '"' lines terminated by '\r' ignore 1 lines;";
# EXECUTE THE QUERY FUNCTION
$execute = $connect->query($myquery);
这是生成的错误:
String found where operator expected at perlfile.pl line 20, near ""load data local infile '/pathToFile/file.csv' replace into table database.table fields terminated by ',' enclosed by '"' lines terminated by '"
(Missing operator before ' lines terminated by '?)
Backslash found where operator expected at perlfile.pl line 20, near "' lines terminated by '\"
(Missing operator before \?)
syntax error at perlfile.pl line 20, near ""load data local infile '/pathToFile/file.csv' replace into table database.table fields terminated by ',' enclosed by '"' lines terminated by '"
Bad name after r' at perlfile.pl line 20.