SQL请求变量引用

SQL request variable quoting

我的 MySQL 查询因引号或遗漏引号而无法运行,但我不明白如何正确使用它。我需要一些解释:

写入 .csv 文件的 Perl 脚本:

open(ECRIRE,">$ARGV[1]") || die ("Impossible de creer le fichier de sortie");

foreach my $key (sort keys %caisse)
{
    print ECRIRE "insert into etablissement(code_etablissement,nom, contact_ce_nom, contact_ce_tel, contact_ce_mail) values ($key,$caisse{$key}[0];$caisse{$key}[1];$caisse{$key}[2];$caisse{$key}[3]) on duplicate key update contact_ce_nom=$caisse{$key}[1],contact_ce_tel=$caisse{$key}[2],contact_ce_mail=$caisse{$key}[3];\n";
}

close(ECRIRE);

Bash 脚本执行 SQL 请求:

$mysql -f -h $db_address -P $db_port -u $db_user -p$db_passwd $db_name < $vtiger_temporary_file_etablissement_clean

Mysql 几乎每一条这样的信息都在哭

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 'migrationrh2cepal@mail.com) on duplicate key update contact_ce_no' at line 1

我什至尝试用单引号引用每个变量,结果相同...

编辑:将 DBI 用于 perl

代码现在看起来像:

foreach my $key (sort keys %caisse)
{
    my $insert = $sql_connection->prepare('insert into etablissement values(?, ?, ?, ?, ?, ?) on duplicate key update');
    $insert->execute($key, $caisse{$key}[0], $caisse{$key}[1], $caisse{$key}[2], $caisse{$key}[3],'');
}

我现在遇到了 "on duplicate key" 问题。如何在此处添加 "on duplicate key" 语句? 我试着在最后添加它,就像这样:

my $insert = $sql_connection->prepare('insert into etablissement values(?, ?, ?, ?, ?, ?) on duplicate key update');

但是没用

不要自己引用变量,而是使用 DBI 模块和占位符:

$db = 'DBI'->connect(...);
my $insert = $db->prepare('insert into etablissement values(?, ?, ?)');
$insert->execute($key, $caisse{$key}[0], $caisse{$key}[1]);

对于重复值,通常使用编号占位符:

my $insert = $db->prepare(<<'__SQL__');
INSERT INTO etablissement
       (code_etablissement, nom, contact_ce_nom, contact_ce_tel, contact_ce_mail)
       VALUES (:1, :2, :3, :4, :5)
       ON DUPLICATE KEY UPDATE contact_ce_nom  = :3,
                               contact_ce_tel  = :4,
                               contact_ce_mail = :5'
__SQL__

$insert->execute($key, @{ $caisse{$key} }[0 .. 3]);

如果您的驱动程序不支持它们(我没有在 DBD::mysql 中看到它们),您可以解决它,例如

my $insert = $db->prepare(<<'__SQL__');
INSERT INTO etablissement
       (code_etablissement, nom, contact_ce_nom, contact_ce_tel, contact_ce_mail)
       VALUES (?, ?, ?, ?, ?)
       ON DUPLICATE KEY UPDATE contact_ce_nom  = ?,
                               contact_ce_tel  = ?,
                               contact_ce_mail = ?
__SQL__

$insert->execute($key, @{ $caisse{$key} }[0 .. 3], @{ $caisse{$key} }[1 .. 3]);

或者您可以使用 quote 并完全省略占位符。

使用来自 choroba 的部分答案:

最后一题是"how to use on duplicate key syntax with perl DBI ?"

喜欢这个

my $insert = $sql_connection->prepare('insert into etablissement values(?, ?, ?, ?, ?, ?) on duplicate key update contact_ce_nom=?,contact_ce_tel=?,contact_ce_mail=?');
    $insert->execute($key, $caisse{$key}[0], $caisse{$key}[1], $caisse{$key}[2], $caisse{$key}[3],'',$caisse{$key}[1],$caisse{$key}[2],$caisse{$key}[3]);

"on udplicate key" 语法使用最后 3 个 ARGS...