0

I am making a HANA application in SAP HANA Studio. I am able to insert entries into a hdbtable that I made when they are normal things like "1" and "apple". When I try to add values such as "+" or "%" for some reason they show up as a space in my data preview. How come they aren't be stored? How can I fix this so I can add these characters? Changing NVARCHAR and VARCHAR doesn't work.

Below is my schema for my table.

table.schemaName = "scheming";
table.tableType = COLUMNSTORE;
table.description = "test table";
table.columns = [
    {name = "id"; sqlType = NVARCHAR; nullable = false; length = 10; comment = "id"; },
    {name = "desc"; sqlType = VARCHAR; nullable = false; length = 10; comment = "desc";}
];

table.primaryKey.pkcolumns = ["id"];

and here's my xsjs file:

$.response.contentType = "text/html";

var id = $.request.parameters.get('id');
var desc = $.request.parameters.get('desc'); 

$.trace.debug("Here is my log.");

try {
    var conn = $.db.getConnection();

    var st = conn.prepareStatement("INSERT INTO \"scheming\".\"blah.blah::test_table\" values(?,?)");

    st.setString(1, id);
    st.setString(2, desc);

    st.execute();

    st.close();
    conn.commit();
    conn.close();

    $.response.setBody('X');

} catch (err) {
    $.response.setBody(err);
    // $.response.status = $.net.http.INTERNAL_SERVER_ERROR
}

controller file

jQuery(document).ready(
                function() {
                    jQuery.get("/com/colpal/training/test1/Insert.xsjs?id=" + id +"&desc=" + desc,
                            function(result) {

                        if (result == 'X'){
                            console.log("Inserted");
                        }else if(result == 'Y'){
                            console.log("Fail");
                        }

                    });
                });
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
SAPUI5GUY
  • 71
  • 1
  • 2
  • 10
  • How do you handover the parameters you want to insert to your script? Via URL parameters? In that case make sure to escape characters, that are special for URLs, such as %, ?, +, : . – Lars Br. Feb 22 '17 at 20:43
  • I added the part of the controller where I handover the parameters. Where exactly should I be doing the check? When I introduce the % as the desc parameter in the controller I think it hands it over okay because I see that `Insert.xsjs?id=17&desc=% ` is retrieved (although it returns a 400 error). I checked in the XSJS `desc==="%25"` and if so I change to just a "%" but I don't think it fixes it. – SAPUI5GUY Feb 22 '17 at 21:06
  • I think you're right but I guess my question is where does the URL do the encoding? A workaround I'm doing is changing the desc in the controller to some defined characters like "perc" and then checking if it's this value in the XSJS file. Is this the best way I can fix this problem? – SAPUI5GUY Feb 22 '17 at 21:16

1 Answers1

0

The snipped of your controller shows that you are passing over id and desc without encoding in the URL. That does not work for characters that have a special meaning in the URL like + and %. You can find details about URL encoding and reserved characters in RFC 3986.

Use JavaScript's encodeURIComponent to encode the individual data when constructing the URL:

jQuery.get("/com/colpal/training/test1/Insert.xsjs?id=" +
    encodeURIComponent(id) + "&desc=" + encodeURIComponent(desc), ...
Community
  • 1
  • 1
djk
  • 943
  • 2
  • 9
  • 27