Search code examples
plsqljax-rsoracle-apex-5

Parsing string in PL/SQL and insert values into database with RESTful web service


I am reading the values which I want to insert into database. I am reading them by lines. One line is something like this:

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

Web service needs to separate values by comma and insert them into database. In PL/SQL language. How do I do that?


Solution

  • I found an answer to that particular question. If you have similar values to those I posted for a question, like numbers, which look something like this:

    String line = "145, 899";
    

    This string is sent via POST request (RESTful web service, APEX). Now getting the values in PL/SQL and inserting them into table looks something like this:

    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;
    

    However, this is the method to insert line by line into database, so if you have large amount of rows in a file to insert, this isn't a way to do it. But here is the example which I requested. I hope it helps to someone.