简体   繁体   English

为什么mysql在似乎没有时会给我一个语法错误?

[英]why mysql gives me a syntax error when there seems to be none?

The following code is runnable and works fine but if I change $dbh->do("set names utf8"); 以下代码是可运行的并且工作正常,但如果我更改$dbh->do("set names utf8"); to $dbh->do("set names gbk"); to $dbh->do("set names gbk"); , I will receive a syntax error: ,我将收到语法错误:

use strict;
use warnings;
use DBD::mysql;

my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost","root","password");
$dbh->do("set names utf8");

open  my $jpg, '<', 'test.jpg' or die "$!";
binmode $jpg;
my $image = do{local $/; <$jpg>};
close $jpg;

 my $sql = "INSERT INTO student VALUES (?, ?)";

 my $sth = $dbh->prepare($sql);
 $sth->bind_param(1, 'Yang Liu');
 $sth->bind_param(2, $image);   
 $sth->execute();
 $sth->finish;
 exit;

The syntax error reads as follows: 语法错误如下:

DBD::mysql::st execute failed: You have an error in your SQL syntax; check the m
anual that corresponds to your MySQL server version for the right syntax to use
near 'id=\'W5M0MpCehiHzreSzNTczkc9d\'?>\n<x:xmpmeta xmlns:x=\'adobe:ns:meta/\' x
:xmptk' at line 1 at D:\sqltest.pl line 22.
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the m
anual that corresponds to your MySQL server version for the right syntax to use
near 'id=\'W5M0MpCehiHzreSzNTczkc9d\'?>\n<x:xmpmeta xmlns:x=\'adobe:ns:meta/\' x
:xmptk' at line 1 at D:\sqltest.pl line 22.

And the suspicious line that reads 'id=\\'W5M0MpCehiHzreSzNTczkc9d\\'?>\\n<x:xmpmeta xmlns:x=\\'adobe:ns:meta/\\' is associated with the test.jpg image file which I'm trying to store to the longblob as I can find the same string if I open the image file using a text editor. 我读的可疑行读为'id=\\'W5M0MpCehiHzreSzNTczkc9d\\'?>\\n<x:xmpmeta xmlns:x=\\'adobe:ns:meta/\\'与我正在尝试的test.jpg图像文件相关联存储到longblob,因为如果我使用文本编辑器打开图像文件,我可以找到相同的字符串。

It's weird that code also works if I change $dbh->do("set names utf8"); 如果我更改$ dbh-> do(“set names utf8”),代码也可以工作,这很奇怪; to $dbh->do("set names gb2312"); to $ dbh-> do(“set names gb2312”); Why the set names gbk statement gives me a syntax error? 为什么set name gbk语句给我一个语法错误? Is there something obvious that I'm doing wrong? 有什么明显的东西我做错了吗?

Thanks in advance:) 提前致谢:)

I'm running ActivePerl 5.10.1 and MySQL 5.5. 我正在运行ActivePerl 5.10.1和MySQL 5.5。

UPDATE UPDATE

I've found the culprit! 我找到了罪魁祸首!

This line local $/; 这行local $/; is causing a MySQL syntax error. 导致MySQL语法错误。 To fix the problem, simply comment out this line. 要解决此问题,只需注释掉这一行。

My Perl script is encoded in GBK and my database table encoding is also GBK, but I have to also add the set names to gbk to the DBI in perl, otherwise, GBK characters inserted into the table won't display properly in a GBK environment. 我的Perl脚本以GBK编码,我的数据库表编码也是GBK,但我还必须将设置名称添加到glk到perl中的DBI,否则插入表中的GBK字符将无法在GBK环境中正确显示。

UPDATE2 UPDATE2

I thought I had fixed the problem but the problem is actually still there. 我以为我已经解决了问题,但问题实际上仍然存在。 When I delete the local $/; 当我删除本地$ /; line, there's no error message but the blob field contains a corrupted image file. 行,没有错误消息,但是blob字段包含损坏的图像文件。

GBK is outdated, you must not use it anymore; GBK已过时,您不能再使用它; and MySQL does not support the current standard GB 18030. 并且MySQL不支持当前的标准GB 18030。

Work-around: do not set names , and deal with encoding of text in Perl only. 解决方法:不要set names ,而仅在Perl中处理文本编码。 Keep binary data such as images as octets. 将二进制数据(例如图像)保留为八位字节。

use utf8;
use Encode qw(encode);
use Encode::HanExtra qw();
⋮
my $name = encode('GB18030', '刘阳', Encode::FB_CROAK | Encode::LEAVE_SRC);
$dbh->do(
    'INSERT INTO student VALUES (?, ?)',
    {},
    $name, $image
);

(I have not tested this code.) (我没有测试过这段代码。)

com/users/183467/mike , com / users / 183467 / mike,

I solved this by using the function addslashes($img). 我使用addslashes($ img)函数解决了这个问题。 Without it the img var contain chars that need to be covered by a slash to allow them in database properly.. Otherwise, it seems such chars would blend with your sql code and get misread as syntax errors. 没有它,img var包含需要用斜杠覆盖的字符,以使它们正确地进入数据库。.否则,似乎这些字符会与您的sql代码混合并被误读为语法错误。

That's why even after you removed the appearance the error message the image was not stored the right way. 这就是为什么即使在删除外观错误消息后,也无法以正确的方式存储图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM