J将我的库存数据放在csv文件中。 我收到一条错误消息,因为我的插入值上有一个特殊字符,我不想处理这些值因为它们被标记为特殊字符$"不再使用它们进行插入。我使用下面的代码:
#!/usr/bin/perl
# PERL MODULES WE WILL BE USING
use DBI;
use DBD::mysql;
# HTTP HEADER
print "Content-type: text/html \n\n";
# CONFIG VARIABLES
$platform = "mysql";
$database = "store";
$host = "localhost";
$port = "3306";
$tablename = "inventory";
$user = "username";
$pw = "password";
# DATA SOURCE NAME
$dsn = "dbi:$platform:$database:$host:$port";
# PERL DBI CONNECT
$connect = DBI->connect($dsn, $user, $pw);
# PREPARE THE QUERY
$query = "INSERT INTO inventory (id, product, quantity) VALUES (DEFAULT, 'tomatoes$"', '4')";
$query_handle = $connect->prepare($query);
# EXECUTE THE QUERY
$query_handle->execute();
如何擦除错误消息并移至下一个插入内容。
答案 0 :(得分:1)
你必须在你的查询中转义$,因为现在它是一个perl语法错误:
$sql = "INSERT .... 'tomatoes$"', '4')";
^----
引用TERMINATES sql字符串,因为它不是有效变量。尝试
$sql = "INSERT .... 'tomatoes\$"', '4')";
^--
代替。
答案 1 :(得分:1)
处理你的插入句子,避免sql-injection:
my $query = qq!INSERT INTO inventory (id, product, quantity) VALUES (?,?,?)!;
my $query_handle = $connect->prepare($query);
$query_handle->execute('DEFAULT', 'tomatoes$', '4');