Select Query - WebSQL
-
22-04-2021 - |
Question
I've the following code to select a row.. When I call the function with getRecords("Peter Sam"); one record is shown.. However if I just pass getRecords("Peter"); it says "No results".
getRecords = function(cname){
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM contacts WHERE (cname LIKE ?)', [cname], renderResults);
});
}
What is the correct usage of "LIKE" in "Select" query?? BTW where do I refer SQL syntax for WebSQL?
Thanks
Solution
In transact SQL you would use a % as a wild card. something like:
SELECT * FROM contacts WHERE cname LIKE ?%
However, WebSQL has been discontinued so I don't suggest using this method.
OTHER TIPS
getRecords = function(cname){
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM contacts WHERE (cname LIKE ?)', ['%'+cname+'%'], renderResults);
});
}
Use string concatenation:
SELECT * FROM contacts WHERE cname LIKE '%' || ? || '%'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow