在PL / SQL中解析字符串,并使用RESTful Web服务将值插入数据库

时间:2015-07-08 12:36:18

标签: plsql jax-rs oracle-apex-5

我正在读取要插入数据库的值。我是按行读的。一行是这样的:

String line = "6, Ljubljana, Slovenija, 28";

Web服务需要用逗号分隔值并将它们插入数据库。在PL / SQL语言中。我该怎么做?

2 个答案:

答案 0 :(得分:1)

这是我用来解析分隔字符串然后提取单个单词的一些pl / sql。使用Web服务时,您可能不得不使用它,但是当您在oracle中运行它时,它可以正常工作。

declare
  string_line varchar2(4000);
  str_cnt number;
  parse_pos_1 number := 1;
  parse_pos_2 number;
  parsed_string varchar2(4000);
begin
  --counting the number of commas in the string so we know how many times to loop
  select regexp_count(string_line, ',') into str_cnt from dual;

  for i in 1..str_cnt + 1
  loop
      --grabbing the position of the comma
      select regexp_instr(string_line, ',', parse_pos_1) into parse_pos_2 from dual;

      --grabbing the individual words based of the comma positions using substr function
      --handling the last loop
      if i = str_cnt + 1 then
        select substr(string_line, parse_pos_1, length(string_line)+1 - parse_pos_1) into parsed_string from dual;

        execute immediate 'insert into your_table_name (your_column_name) values (' || parsed_string || ' )';
        execute immediate 'commit';
      --handles the rest
      else
        select substr(string_line, parse_pos_1, parse_pos2 - parse_pos_1) into parsed_string from dual;
        execute immediate 'insert into your_table_name (your_column_name) values (' || parsed_string || ' )';
        execute immediate 'commit';
      end if;

      parse_pos_1 := parse_pos_2+1;

   end loop;
end;

答案 1 :(得分:0)

我找到了这个特定问题的答案。如果你有类似于我为一个问题发布的值,比如数字,它们看起来像这样:

String line = "145, 899";

此字符串通过POST请求(RESTful Web服务,APEX)发送。现在获取PL / SQL中的值并将它们插入表中就像这样:

DECLARE
    val1 NUMBER;
    val2 NUMBER;
    str CLOB;
BEGIN
    str := string_fnc.blob_to_clob(:body);                   // we have to convert body
    val1 := TO_NUMBER(REGEXP_SUBSTR(str, '[^,]+', 1, 1));
    val2 := TO_NUMBER(REGEXP_SUBSTR(str, '[^,]+', 1, 2));
    // REGEXP_SUBSTR(source, pattern, start_position, nth_appearance)
INSERT INTO PRIMER VALUES (val1, val2);

END;

但是,这是逐行插入数据库的方法,因此如果要插入的文件中有大量行,则无法执行此操作。但这是我要求的例子。我希望对某人有所帮助。