1

I'm trying to pass a dynamic array to a executeSql. I'm passing the SQL to the IN as array.

let lor_text_Array = [];
if (lor_text == "TEST 1, 2, 3") {
    lor_text_Array = ["TEST 1", "TEST 2", "TEST 3"];
}
else {
    lor_text_Array.push("TEST 4")
    lor_text_Array.push("TEST 5")
}

let sql_query = 'SELECT * FROM SUBMISSION WHERE REV IN (' + lor_text_Array + ')ORDER BY ID DESC'; 
app.db.transaction(function (tx) { tx.executeSql(sql_query, [], loadItemsSYNCED, app.onError);});

however an error occured while executing sql Error object Object

1
  • You should format your array as a string value manually. Because it's formatted by JS as object Object. Commented Dec 3, 2019 at 23:38

1 Answer 1

3

Assuming that your string values do not contain embedded single quotes, you use map and join to generate the IN list, as follows:

let lor_in_list = lor_text_Array.map(function (a) { return "'" + a + "'"; }).join(",");
let sql_query = 'SELECT * FROM SUBMISSION WHERE REV IN (' + lor_in_list + ')ORDER BY ID DESC';

To handle possible embedded single quotes:

let lor_in_list = lor_text_Array.map(function (a) { 
    return "'" + a.replace("'", "''") + "'"; 
}).join(",");
Sign up to request clarification or add additional context in comments.

2 Comments

ok you mean when i create the array like let lor_text_Array = ["TEST 1", "TEST 2", "TEST 3"]; it is not a comma seperated string?
can i avoid single quotes when creating the array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.