Based on this tutorial, there is a function called "fetchCountriesByName" to filer the listview content by comparing "KEY_NAME" with inputted country name. Only matching "KEY_NAME" record will be displayed.
http://www.mysamplecode.com/2012/07/android-listview-cursoradapter-sqlite.html
public Cursor fetchCountriesByName(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION}, null, null, null, null, null);
}
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
KEY_NAME + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
My question is how to fetch from multiple field which similar to inputted text? I want the inputted text not just compare with KEY_NAME, but also compare with KEY_REGION. I had added the "KEY_REGION" in the bold area, but it doesn't work. How can I fetch KEY_REGION & KEY_NAME?
public Cursor fetchCountriesByName(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
null, null, null, null, null);
}
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
KEY_NAME+ KEY_REGION + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Use the boolean logical operators AND or OR.
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
KEY_NAME + " like '%" + inputText + "%' AND "+ KEY_REGION + " like '%" + inputText + "%'", null,
null, null, null, null);
Try this:
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
KEY_NAME+" like '%?%' OR "+KEY_REGION+" like '%?%'", new String[]{inputText, inputText},
null, null, null, null);
The bound variables (?) will protect you from SQL injection attacks. You will also want to consider changing KEY_ROWID to _ID, as this is what ListView expects.
Related
I'm using filtering and the only filter that is implemented is a search by key_name, I'd like to also add other keys.
public static Cursor fetchCountriesByName(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor c = null;
if (inputText == null || inputText.length () == 0) {
c = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_COUNTRY, KEY_REGION, KEY_PHONE},
null, null, null, null, null);
}
else {
c = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_COUNTRY, KEY_REGION, KEY_PHONE},
KEY_NAME + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (c != null) {
c.moveToFirst();
}
return c;
}
when i tried to use else { if ( it requested a (c != null), when i tried to modify, KEY_NAME + " like '%" + inputText + "%'", null, KEY_COUNTRY + " like '%" + inputText + "%'",KEY_REGION + " like '%" + inputText + "%'"); was saying illegalargumentexception.
How to do it right?
Thanks.
you forgot to add condition like ANDor OR in your query
check below example
db.query(true, DATABASE_TABLE,new String[] {KEY_ROWID,
KEY_NAME, KEY_COUNTRY, KEY_REGION, KEY_PHONE},
KEY_NAME + " LIKE ? AND ", KEY_COUNTRY + " LIKE ? AND ", KEY_REGION + " LIKE ?",
new String[] {"%"+inputText+"%" ,"%"+inputText+"%" ,"%"+inputText+"%"},
null, null, null, null);
here i have used AND you can replace it as your need.
i added a filter to my listview ( populated from sqlite ). i am using this method to fetch the records from sqlite
public Cursor fetchServicesByName(String inputText, String c) throws SQLException {
db = this.getReadableDatabase();
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = db.query(TABLE_SERVICE, new String[] {KEY_ID,KEY_NAME,
KEY_ADRESSE, KEY_EMAIL, KEY_NUMTEL, KEY_WEBSITE,KEY_IMAGE}, KEY_TYPE + "=?",
new String[] { c },null, null, null, null);
}
else {
mCursor = db.query(true, TABLE_SERVICE, new String[] {KEY_ID,KEY_NAME,
KEY_ADRESSE, KEY_EMAIL, KEY_NUMTEL, KEY_WEBSITE,KEY_IMAGE},
KEY_NAME + " like '%" + inputText + "%'" , null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
this is working, but what i want to do is to filter by inputText and KEY_TYPE
i used this (instead of the last db.query ) but it didn't return anything
mCursor = db.query(TABLE_SERVICE, new String[] {KEY_ID,KEY_NAME,
KEY_ADRESSE, KEY_EMAIL, KEY_NUMTEL, KEY_WEBSITE,KEY_IMAGE},
KEY_NAME + " like ? and" + KEY_TYPE + " like ? " , new String[] { inputText, c},
null, null, null, null);
You need to check if your inputtext and keytype are null!
Aris your routine working when you fill out your inputtext and c vallue?
In my code I have a database and when I retrieve rows with KEY_DESC = userselectvalue , it worked. But when I tried to retrieve rows with multiple 'where' conditions it failed. I used to show the retrieved elements in ListView. Please help me. No errors are showing. But the ListView is blank.
public Cursor fetchEventByName(String inputText,String inputText1) throws SQLException {
//Log.w(TAG, inputText);
SQLiteDatabase db = this.getReadableDatabase();
Cursor mCursor = null;
if ((inputText == null || inputText.length () == 0) && (inputText1 == null || inputText1.length () == 0 ) ){
mCursor = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_DESC, KEY_EVENT, KEY_DATE },
null, null, null, null, null);
}
else {
String [] temp = new String[] {KEY_ROWID,
KEY_DESC, KEY_EVENT, KEY_DATE};
mCursor = db.query(false,DATABASE_TABLE,temp ,
KEY_DESC + " = '" + inputText + "'" + " AND " + KEY_DATE + " = '" + inputText1+ "'", null, null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
You could try db.rawQuery by passing sql queries directly.
In my code there is a function that fetches rows from database with specific column value. Here it is KEY_DESC. I also need to add the additional condition of specific value for KEY_DATE. How can I do that?
fetchEventByName()
public Cursor fetchEventByName(String inputText,String inputText1) throws SQLException {
//Log.w(TAG, inputText);
SQLiteDatabase db = this.getReadableDatabase();
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_DESC, KEY_EVENT, KEY_DATE },
null, null, null, null, null);
}
else {
mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_DESC, KEY_EVENT, KEY_DATE},
KEY_DESC + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Did you try something like this?
mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_DESC, KEY_EVENT, KEY_DATE},
KEY_DESC + " like '%" + inputText + "%' or "+KEY_DATE+"="+WHAT_EVER, null,
null, null, null, null);
How would i go about querying more than one selection arg? For example here is how my database is formatted
Here is the code i used to search with just one seletion arg:
public Cursor getType(String type) throws SQLException
{
Cursor mCursor =
db.query(true, DB_TABLE, new String[] {
KEY_ROWID,
KEY_ALCOHOL,
KEY_TYPE,
KEY_BRAND,
KEY_PRICE
},
KEY_TYPE + "=?",
new String[] { type },
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
But this only searches by KEY_TYPE, how would I set it so it searches by KEY_TYPE, KEY_ALCOHOL, and KEY_PRICE?
This may help you
public Cursor getType(String type) throws SQLException
{
Cursor mCursor =
db.query(true, DB_TABLE, new String[] {
KEY_ROWID,
KEY_ALCOHOL,
KEY_TYPE,
KEY_BRAND,
KEY_PRICE
},
KEY_TYPE + "=?" + " AND " + KEY_ALCOHOL + "=?" " AND " + KEY_PRICE + "=?",
new String[] { type,alcohol,price },
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Figured it out!! Thanks to #Rajendra for giving me some code to build off of! You can only add Strings into the selections args so i did this:
public Cursor getTest(String alcohol, String type, long price) throws SQLException
{
Cursor mCursor =
myDataBase.query(true, DB_TABLE, new String[] {
KEY_ROWID,
KEY_ALCOHOL,
KEY_TYPE,
KEY_BRAND,
KEY_PRICE
},
KEY_ALCOHOL + "=?" + " AND " + KEY_TYPE + "=?" + " AND " + KEY_PRICE + "<=" + price,
new String[] { alcohol, type},
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
And it works!!