mysql - 将json字符串插入mysql

标签 mysql json perl dbi

我想在 mysql 数据库中存储 json 字符串。 但似乎有一些报价问题,我无法弄清楚。

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 ;

最佳答案

尝试删除 meta 后右括号后的句点。

关于mysql - 将json字符串插入mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36009514/

相关文章:

php - MySQL 数据透视表子字符串列名称

mysql - 显示包含最新编辑版本的所有记录

php - 如何根据选择选项更改表数据

Android:将 MySQL 数据库中的数据存储在 ArrayList 中

json - JSON 数组字段的索引

sql - 从 DBIx::Class 中的多个表中选择

arrays - Perl:映射到列表的第一个元素

java - 压缩netty发送的数据

python - 我可以使用 Django 的模板语言来构造 HTML 以在 JSON 响应字段中使用吗?

perl - Schedule::Cron 永远不会返回主脚本