sqlite - Search two or more DB columns with Android Search Dialog -
i have 3 column db android search dialog working correctly. i'd able type in work or phrase 2 different columns , have return results searched phrase either in 1 or other column, or both columns. i'm using below method:
//--- records search public cursor searchdb(string query) { return db.query(true, db_table, new string[] { key_rowid, key_column1, key_column2, key_column3 }, key_column1 + " like" + "'%" + query + "%' or " + key_column2 + " like" + "'%" + query + "%' or " + key_column3 + " like" + "'%" + query + "%'", null, null, null, null, null); } //--- end records search so works fine if have db this:
col1 col2 col3 ______________________ hi there guys how and searched on "hi" return "hi there guys". want search on "hi there" , have return whole row of 3 columns - "hi there guys". tried adding:
or "+ key_column2 + key_column3 + " like" + "'%" + query + "%'" but crashes search when search run.
i think should apply 1 more step before querying database.
you should split query string in array of words.
then solution use selectionargs parameter pass words of query:
string[] querywords = splitinwords(query); // own method split string in words db.query(true, db_table, new string[] { key_rowid, key_column1, key_column2, key_column3 }, key_column1 + " like" + " ? or " + key_column2 + " like" + " ? or " + key_column3 + " like" + " ?", querywords, null, null, null, null); the problem previous 1 you're supposed have 3 words (i have never tried don't know sure result you'll get). solution create selection string:
string[] querywords = splitinwords(query); // own method split string in words string selection = ""; string[] keycolumns = new string[]{key_column1, key_column2, key_column3}; (int = 0; < querywords.length; i++){ if ( > 1){ selection += " or "; } selection += keycolumns[i] + " " + querywords[i]; } well might not you're looking you'll adapt depending on expect queried (you can build longer selection string pairs key/queryword). , never found better idea handle kind of queries.
Comments
Post a Comment