2016-12-30 104 views
0

我试图用perl中插入一个二进制文件(JPG图像)的MySQL的数据库当我尝试将值插入名为“index”的列的表中时,为什么会出现SQL语法错误?

表:

CREATE TABLE `images` (
    `sku` CHAR(12) NOT NULL, 
    `index` TINYINT(1) UNSIGNED NOT NULL, 
    `main` BLOB NULL 
) 
COLLATE='utf8_general_ci' 
ENGINE=InnoDB 
ROW_FORMAT=DYNAMIC 
; 

的Perl:

$dbh_local = DBI->connect("DBI:mysql:database=db;host=127.0.0.1;mysql_enable_utf8=1", "XXX", "XXX", {'RaiseError' => 1, 'mysql_auto_reconnect' => 1}); 

open IMAGE, "c:/image.jpg" or die $!; 
    while(read IMAGE, $buff, 1024) { 
     $image .= $buff; 
    } 
close(IMAGE); 

my $sku = 'VM1000032999'; 
my $index = 1; 

$query = "INSERT INTO images (sku,index,main) values (?,?,?)"; 
$sth = $dbh_local->prepare($query); 
$sth->bind_param(1,$sku); 
$sth->bind_param(2,$index); 
$sth->bind_param(3,$image, DBI::SQL_BLOB); 
$sth->execute();   
$sth->finish(); 

但是我得到这个错误:

"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 'index,main) values ('VM1000032999','1','????\0►JFIF\0☺☺☺\0H\0H\0\0??\0?\0♠♦♣♠♣♦♠' at line 1 at ...." 

任何想法?我尝试了几个变化,都给出了相同的错误。

+3

索引是保留字。 https://dev.mysql.com/doc/refman/5.5/en/keywords.html – user3606329

+1

您不能将列索引作为保留字。它在引用的create table中起作用。要么总是引用它,要么选择一个不保留的单词 – KeepCalmAndCarryOn

+1

'index'是一个mysql关键字。最好不要使用关键字作为字段名称,但如果这样做,则需要像在create table语句中那样转义它们。 –

回答

相关问题