2

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

Naveen
  • 1,040
  • 4
  • 15
  • 38

3 Answers3

3
getRecords = function(cname){
    db.transaction(function(tx) {
        tx.executeSql('SELECT * FROM contacts WHERE (cname LIKE ?)', ['%'+cname+'%'], renderResults);
    });
}
Muhaimin CS
  • 195
  • 2
  • 19
2

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.

tnt-rox
  • 5,400
  • 2
  • 38
  • 52
  • 3
    Thanks for the response. The following query worked for me, SELECT * FROM contacts WHERE (cname LIKE ?);', ["%"+cname+"%"], renderResults Also do you suggest me to use indexedDB instead of WebSQL?? If so, is indexedDB is supported in Android 2.3?? – Naveen Feb 05 '12 at 07:27
0

Use string concatenation:

SELECT * FROM contacts WHERE cname LIKE '%' || ? || '%'
SystemParadox
  • 8,203
  • 5
  • 49
  • 57