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 ...."
任何想法?我嘗試了幾個變化,都給出了相同的錯誤。
索引是保留字。 https://dev.mysql.com/doc/refman/5.5/en/keywords.html – user3606329
您不能將列索引作爲保留字。它在引用的create table中起作用。要麼總是引用它,要麼選擇一個不保留的單詞 – KeepCalmAndCarryOn
'index'是一個mysql關鍵字。最好不要使用關鍵字作爲字段名稱,但如果這樣做,則需要像在create table語句中那樣轉義它們。 –