2016-03-15 144 views
1

我想將json字符串存儲在mysql數據庫中。 但似乎有一些報價問題,我想不通。將json字符串插入到mysql中

use DBI; 
use JSON; 

#some staff 

my $json_string = encode_json \%DB; 

#DBI CONNECTION... 

# insert data into the links table 
my $sql = "INSERT INTO `tech_nodes` (`meta`). 
    VALUES(?)"; 
my $stmt = $dbh->prepare($sql); 
$stmt->bind_param(1, $json_string); 

# execute the query 
$stmt->execute(); 

錯誤:

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 '. VALUES('{\"techs\":{\"A\":{\"B\":null,\"techs\":{\"stack.tc' at line 1 at ./stacks.pl line 121. 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 '. VALUES('{\"techs\":{\"A\":{\"B\":null,\"techs\":{\"stack.tc' at line 1 at./stacks.pl line 121.

表描述:

CREATE TABLE IF NOT EXISTS `tech_nodes` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `meta` MEDIUMTEXT NOT NULL, 
    `generated_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 

回答

2

嘗試meta之後去除所述右括號後的那段時間。

+0

哦,很好。 –