【问题标题】:Insert into JSON column postgres [duplicate]插入 JSON 列 postgres [重复]
【发布时间】:2018-08-06 06:40:02
【问题描述】:

我有一个这样的 JSON 条目,我需要将它输入到一个列中(称为值)

["Price Descending","Price Ascending","Name Ascending","Date Descending"]

如何将它输入到 json 列中,我正在尝试的是这个

 $this->db->query("
       INSERT INTO tag_sets (type, value) VALUES
            ('sorting_options', " 
            'Price Descending', 'Price Ascending', 'Name Ascending', 
            'Date Descending'";
      ");

这给了我一个错误,插入 json 列的正确方法是什么?

【问题讨论】:

  • 我们需要知道错误是什么。
  • 什么是“json 列”?是否要将 JSON 存储在一个数据库字段中?
  • @bart 是的,先生,是的
  • 我得到语法错误

标签: php json postgresql codeigniter


【解决方案1】:

试试这个

$json = addslashes('"sorting_options",  "Price Descending", "Price Ascending", "Name Ascending", "Date Descending"');

$this->db->query("INSERT INTO tag_sets (type, value) VALUES ('sorting_options', '{$json}')");

【讨论】:

    【解决方案2】:

    PostgreSQL 无法将字符串转换为 json。您必须在执行查询之前调整 JSON 语法。

    $this->db->query("
           INSERT INTO tag_sets (type, value) VALUES
                ('sorting_options', " 
                '[\"Price Descending\", \"Price Ascending\", \"Name Ascending\", \"Date Descending\"]'";
          ");
    

    尝试执行它。

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 2018-01-12
      • 2015-08-13
      相关资源
      最近更新 更多