Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用perl将特殊字符插入mysql_Mysql_Perl - Fatal编程技术网

使用perl将特殊字符插入mysql

使用perl将特殊字符插入mysql,mysql,perl,Mysql,Perl,嗨,我对perl很陌生。。 我有一个像这样的temp_data.txt文件 Id Comments -------------------------------- 1 this is a 'comment' 2 special comment 3 user comment 'user' ----------------------------------- open (MYFILE, 'temp_data.txt'); while (<MYFI

嗨,我对perl很陌生。。 我有一个像这样的temp_data.txt文件

Id    Comments
--------------------------------
1      this is a 'comment'
2      special comment
3      user comment 'user'
-----------------------------------


  open (MYFILE, 'temp_data.txt');
while (<MYFILE>) {
if($_=~/^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/)
{
    $id=$1;    
    $comment = $2;
}

while(<MYFILE>)
{
    $line=$_;
    if($line=~/^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/)
    {
        seek(MYFILE, -length($_), 1);
        last;
}
    else
    {           if($_=~/\s*(.*)/)
        {
            $comment .=$1;

        }
    }
}

my $queryString = "INSERT INTO Headline (id,comment) VALUES ('$id', ' $comment')";

$sth = $dbh->prepare($queryString);
$sth->execute() or die $DBI::errstr;
$sth->finish();
}
有人能帮我吗?
提前感谢

也许,您插入的数据有特殊符号。为此使用参数化查询(它将保护您免受SQL注入):

DBD::mysql::st execute failed: 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 'comment')' at line 1 at head.pl line 1.
my $queryString = "INSERT INTO Headline (id,comment) VALUES (?, ?)";

$sth = $dbh->prepare($queryString);
$sth->execute($id, $comment) or die $DBI::errstr;
$sth->finish();