繁体   English   中英

尝试将值插入具有“索引”列的表时,为什么会出现SQL语法错误?

[英]Why do I get a SQL syntax error when I try to insert values into a table with a column named “index”?

我正在尝试使用perl将二进制文件(JPG图像)插入到MySQL数据库中

表:

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 ...."

有任何想法吗? 我尝试了几种变体,都给出了相同的错误。

您正在INSERT INTO语句中使用保留字index 第9.2节“模式对象名称”中所述, 仅允许使用带引号的保留字作为标识符。

INSERT INTO语句`index` index更改为`index`

我不建议在表中使用保留字。

暂无
暂无

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

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