retrieving rows with multiple column values sqlite - android

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);

Related

IllegalArgumentException in filtering ListView

I am using this code to filter my listview:
Cursor fetchCountriesByName(String inputText) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
if (inputText == null || inputText.length () == 0) {
String[] columns = new String[] {DBHelper.ID,DBHelper.FNAME,DBHelper.LNAME,DBHelper.ADDRESS };
cursor = db.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);
}
else {
cursor = db.query(true, TABLE_NAME, new String[] {DBHelper.ID,
DBHelper.FNAME,DBHelper.LNAME},DBHelper.ADDRESS+ " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
I got an error
java.lang.IllegalArgumentException: column 'patient_address' does not exist
But the column names are same. There is a column called patient_address
What's the reason of this error? Please guide.
Try this
cursor = db.query(true, TABLE_NAME, new String[] {DBHelper.ID,
DBHelper.FNAME,DBHelper.LNAME},DBHelper.ADDRESS+ " like '%?%'", new String[]{inputText },
null, null, null, null);
the parameter is error.

filter a listview populated from SQLite

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?

Multiple where clause failed

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.

Android: SQLite, How to fetch from multiple field?

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.

Setting multiple Selections args in SQLite database query

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!!

Categories

Resources