将数组参数传递给 SQL 命令 javascript
Passing Array Parameter to SQL command javascript
我正在尝试将动态数组传递给 executeSql。我将 SQL 作为数组传递给 IN。
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);});
然而执行时发生错误sql错误对象对象
假设您的字符串值不包含嵌入的单引号,您使用 map
和 join
生成 IN
列表,如下所示:
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';
处理可能的嵌入单引号:
let lor_in_list = lor_text_Array.map(function (a) {
return "'" + a.replace("'", "''") + "'";
}).join(",");
我正在尝试将动态数组传递给 executeSql。我将 SQL 作为数组传递给 IN。
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);});
然而执行时发生错误sql错误对象对象
假设您的字符串值不包含嵌入的单引号,您使用 map
和 join
生成 IN
列表,如下所示:
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';
处理可能的嵌入单引号:
let lor_in_list = lor_text_Array.map(function (a) {
return "'" + a.replace("'", "''") + "'";
}).join(",");