I’m writing a quiz app using phonegap & jquery. I have a number of categories of question from which the user can select (via checkboxes), and want to build a database query to pull back the questions from the relevant categories based on their selection. The code below gives the gist of what I’m trying to achieve, but I cannot pass queryString into the executeSql() method. Is there a straightforward way of doing this that I’m missing? Thanks, Nick
function queryDB4(tx){
var queryString = "SELECT * FROM SBA_TABLE WHERE (";
if (toggle_cardiology == 1)
{
queryString += "cat_cardiology = 1 AND";
}
else if (toggle_respiratory == 1)
{
queryString += "cat_respiratory = 1"
}
// repeat for other categories
queryString += ") LIMIT 10;";
//alert(queryString);
tx.executeSql(queryString, [], querySuccess3, errorCB);
}
I think the problem is that your query string is invalid. Here’s one example string that your code could produce:
There are a number of problems:
1=1(or any other statement that is always true) to handle the trailingAND.elseclause to allow combinations of checkboxes, not just the first one that is checked.ANDto avoid producingANDcat_respiratory.Putting it together: