Skip to Content

Boolean in Prepare Statement in HANA XSJS

Hi Experts,

I have a requirement to pass boolean value while inserting record to table in XSJS. Please let me know what method would be used in this case as setString wont work for boolean.

Thanks,

Mansi

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • Best Answer
    Dec 03, 2018 at 06:10 PM

    As the XSJS db interfaces do not support booleans to my knowledge you have to pass the boolean values as integers (0 = false, 1 = true as described here).

    A simple example using the $.db legacy interface (which you are using I think cause you are talking about setter methods). In that case you need to set the boolean value with method setInteger of the prepared statement object:

    let conn = $.db.getConnection();
    try {
        let stmt = conn.prepareStatement('INSERT INTO "MISC"."MISC_BOOLEAN" values (?,?)');
        stmt.setInteger(1, 8);
        stmt.setInteger(2, 1);  // 0 = false, 1 = true
        stmt.executeUpdate();
        conn.commit();
        $.reponse.contentType = "text/plain";
        $.response.setBody('ok');
    }catch(e) {
        $.response.contentType = "application/json";
        $.response.setBody(JSON.stringify(e));
    }
    

    The same using the $.hdb interface:

    let conn = $.hdb.getConnection();
    try {
        conn.executeUpdate('INSERT INTO "MISC"."MISC_BOOLEAN" values (?,?)', 6, 0);  // second column is a boolean: set 0 for false or 1 for true
        conn.commit();
        conn.close();
        $.reponse.contentType = "text/plain";
        $.response.setBody('ok');
    }catch(e) {
        $.response.contentType = "application/json";
        $.response.setBody(JSON.stringify(e));
    }
    

    Regards,
    Florian

    Add comment
    10|10000 characters needed characters exceeded