如何将(批量)Handsontable Json 数组插入到 oracle 表中?

Posted

技术标签:

【中文标题】如何将(批量)Handsontable Json 数组插入到 oracle 表中?【英文标题】:How to insert(bulk) Handsontable Json array into a oracle table? 【发布时间】:2017-09-30 01:30:06 【问题描述】:
var hot = new Handsontable(container, 
JSON.stringify(data: hot.getData())

JSON:  data:  [
      "col_1" // These are the table columns 
      "col_2"     
    ],
    [
      "test2",// there are values
      "201702"      
    ],
    [
      "test2",// there are values
      "201702"
    ]

这里的列总是动态的。我需要相应地构建插入查询。可以帮助我如何将可操作的数据插入表中。

【问题讨论】:

如果列是“始终动态的”,那么您可能每次都需要不同的表。这就是你的想法吗?加载这些数据后,您将如何处理它?为什么不将 JSON 文档存储为 CLOB? 基于列名,我需要对表构建插入查询。每次我都会插入同一个表,但如果用户只选择一个列,则加载不太正确。我的查询就像这样插入表(col_1,col2)值(“test2”,“201702”)等等.. 所以你的桌子是通用的?所有电子表格的单个表格?您如何知道哪些行属于同一行?你如何存储电子表格的列名? 这不是解决方案吗:***.com/questions/42702150/… 我使用 JSON_TABLE 概念解决了这个问题。将尽快发布解决方案。谢谢大家 【参考方案1】:

这在 12.1.0.2.0 中对我有用

SELECT * 
FROM   JSON_TABLE (' [["USD" , "GBP", 1.2],
                  ["USD" , "EUR", 1.1],
                  ["GBP" , "EUR",  .9]]', '$[*]'  columns ( 
                                          c varchar2(100) path '$[0]',
                                          tc varchar2(100) path '$[1]',
                                          cr number path '$[2]'));

【讨论】:

【参考方案2】:

您的 JSON 对初学者无效..

但是,如果我假设您打算拥有一个数组数组,那么这样的东西应该适合您

SQL> create table SOURCE_DOCUMENTS (
  2    ID NUMBER,
  3    JSON_DOC CLOB CHECK (JSON_DOC IS JSON)
  4  )
  5  /

Table created.

SQL> insert into SOURCE_DOCUMENTS values (1,'
  2  "data": [
  3  [
  4  "col_1", "col_2"
  5  ], [
  6  "test2", "201702"
  7  ], [
  8  "test2", "201702"
  9  ]
 10  ]
 11  ')
 12  /

1 row created.

SQL> commit
  2  /

Commit complete.

SQL> create or replace procedure insertFromJSON(P_ID NUMBER)
  2  AUTHID CURRENT_USER
  3  IS
  4    JSON_DOC         CLOB;
  5    JO               JSON_OBJECT_T;
  6    DATA             JSON_ARRAY_T;
  7    COLUMN_DETAILS   JSON_ARRAY_T;
  8    I                PLS_INTEGER := 0;
  9    COLUMN_LIST      VARCHAR2(32767) := '';
 10    COLUMN_PATTERN   VARCHAR2(32767) := '';
 11    STATEMENT        VARCHAR2(32767) := '';
 12  begin
 13
 14    SELECT JSON_DOC
 15      into JSON_DOC
 16      from SOURCE_DOCUMENTS
 17     where ID = P_ID;
 18
 19    JO   := JSON_OBJECT_T(JSON_DOC);
 20    DATA := TREAT(JO.get('data') as JSON_ARRAY_T);
 21    COLUMN_DETAILS := TREAT(DATA.get(0) as JSON_ARRAY_T);
 22    DBMS_OUTPUT.put_line('Column Count = ' || COLUMN_DETAILS.get_size());
 23    WHILE (i < COLUMN_DETAILS.get_size()) LOOP
 24      COLUMN_LIST := COLUMN_LIST || ',' || '"' || COLUMN_DETAILS.get_string(i) || '"';
 25      COLUMN_PATTERN := COLUMN_PATTERN || ',' || '"' || COLUMN_DETAILS.get_string(i) || '"' || ' VARCHAR2(32) PATH ''
$[' || i || ']''';
 26      i := i + 1;
 27    end loop;
 28    COLUMN_LIST := SUBSTR(COLUMN_LIST,2);
 29    COLUMN_PATTERN := SUBSTR(COLUMN_PATTERN,2);
 30    -- COLUMN_PATTERN := 'IDX FOR ORDINALITY ' || COLUMN_PATTERN;
 31    STATEMENT := 'CREATE TABLE TEST as select ' || COLUMN_LIST || ' from ( select ROWNUM ROW_NUM, ' || COLUMN_LIST ||
 ' FROM SOURCE_DOCUMENTS, JSON_TABLE(JSON_DOC,''$.data[*]'' columns ' || COLUMN_PATTERN || ') where ID = ' || P_ID || ')
 WHERE ROW_NUM > 1';
 32    DBMS_OUTPUT.put_line('Statement = ' || STATEMENT);
 33    EXECUTE IMMEDIATE STATEMENT;
 34  end;
 35  /

Procedure created.

SQL> show errors
No errors.
SQL> --
SQL> set serveroutput on
SQL> /

Procedure created.

SQL> call insertFromJSON(1)
  2  /
Column Count = 2
Statement = CREATE TABLE TEST as select "col_1","col_2" from ( select ROWNUM
ROW_NUM, "col_1","col_2" FROM SOURCE_DOCUMENTS, JSON_TABLE(JSON_DOC,'$.data[*]'
columns "col_1" VARCHAR2(32) PATH '$[0]',"col_2" VARCHAR2(32) PATH '$[1]') where
ID = 1) WHERE ROW_NUM > 1

Call completed.

SQL> commit
  2  /

Commit complete.

SQL> select *
  2    from TEST
  3  /

col_1                            col_2
-------------------------------- --------------------------------
test2                            201702
test2                            201702

SQL>

【讨论】:

感谢详细解释。我正在尝试运行您的 proc 但出现编译错误。我知道为什么会这样错误:PLS-00201:必须声明标识符 'JSON_OBJECT_T' 行:5 文本: JO JSON_OBJECT_T;错误:PL/SQL:忽略项目行:5 文本:JO JSON_OBJECT_T;错误:PLS-00201:必须声明标识符“JSON_ARRAY_T”行:6 文本:DATA JSON_ARRAY_T; 抱歉,应该说明此解决方案需要 Oracle 12.2.0.1.0。此版本新增了对 PL/SQL JSON 的支持。【参考方案3】:

这是一个应该在 12.1.0.2.0 中工作的替代方法

SQL> create or replace procedure insertFromJSON(P_ID NUMBER)
   2   AUTHID CURRENT_USER
   3   IS
   8     I                PLS_INTEGER := 0;
   9     COLUMN_LIST      VARCHAR2(32767) := '';
  10     COLUMN_PATTERN   VARCHAR2(32767) := '';
  11     STATEMENT        VARCHAR2(32767) := '';
  12
  13     CURSOR COL_CURSOR
  14     is
  15     select COLUMN_NAME
  16       from SOURCE_DOCUMENTS,
  17            JSON_TABLE(
  18              JSON_DOC,
  19              '$.data[0][*]'
  20              columns
  21                COLUMN_NAME VARCHAR2(32) path '$'
  22            );
  23
  24   begin
  25
  26    for C in COL_CURSOR LOOP
  27      COLUMN_LIST := COLUMN_LIST || ',' || '"' || c.COLUMN_NAME || '"';
  28      COLUMN_PATTERN := COLUMN_PATTERN || ',' || '"' || c.COLUMN_NAME || '"' || ' VARCHAR2(32) PATH '' $[' || i || '
 ''';
  29      i := i + 1;
  30    end loop;
  31
  32    COLUMN_LIST := SUBSTR(COLUMN_LIST,2);
  33    COLUMN_PATTERN := SUBSTR(COLUMN_PATTERN,2);
  34    STATEMENT := 'CREATE TABLE TEST as select ' || COLUMN_LIST || ' from ( select ROWNUM ROW_NUM, ' || COLUMN_LIST |
  ' FROM SOURCE_DOCUMENTS, JSON_TABLE(JSON_DOC,''$.data[*]'' columns ' || COLUMN_PATTERN || ') where ID = ' || P_ID || '
  WHERE ROW_NUM > 1';
  35    DBMS_OUTPUT.put_line('Statement = ' || STATEMENT);
  36    EXECUTE IMMEDIATE STATEMENT;
  37   end;
  38  /

 Procedure created.

 SQL> drop table TEST
   2  /

 Table dropped.

 SQL> call insertFromJSON(1)
   2  /

 Call completed.

 SQL> commit
   2  /

 Commit complete.

 SQL> select *
   2    from TEST
   3  /

 col_1                            col_2
 -------------------------------- --------------------------------
 test2                            201702
 test2                            201702

【讨论】:

以上是关于如何将(批量)Handsontable Json 数组插入到 oracle 表中?的主要内容,如果未能解决你的问题,请参考以下文章

如何从handsontable返回JSON格式的数据?

将 HandsOnTable 数据自动保存到 JSON

使用handsontable将数据保存到json文件

如何将 Handsontable 数据转换回 C# 类

如何将 Handsontable 数据发布到 WordPress REST API?

通过 JSON 将数据从 handsontable 检索到 Struts2 Action 不起作用