How to get only two fields data from database? - android

I've got code like this which displays every record that got using KEY_CODE. But i want to display records having only KEY_CODE and KEY_NAME which i inserted. What i need to do?
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
KEY_CODE + " like '%" + inputText + "%'", null,
null, null, null, null);
}

try this one
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
KEY_CODE + " like '%" + inputTextForKEY_CODE + "%' AND" + KEY_NAME + " like '%" + inputTextForKEY_NAME + "%'" , null,
null, null, null, null);
}

Try this:
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
KEY_CODE + " like '%" + inputText + "%' AND " + KEY_NAME + " like '%" + inputName + "%'", null,
null, null, null, null);
}
If you want a exact match for Code and Name donĀ“t use LIKE use =
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION},
KEY_CODE + " = '" + inputText + "' AND " + KEY_NAME + " = '" + inputName +"'", null,
null, null, null, null);
}

In SQL, you can build logical expressions with AND or OR.
If you have to inputs that you want to match to the respective fields (i.e., both must match), you should use AND:
mCursor = mDb.query(true, SQLITE_TABLE,
new String[] { KEY_ROWID, KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION },
KEY_CODE + " LIKE ? AND " + KEY_NAME + " LIKE ?",
new String[] { "%"+inputText+"%", "%"+inputName+"%" },
null, null, null, null);
If you have a single input that could match in either field, you should use OR:
mCursor = mDb.query(true, SQLITE_TABLE,
new String[] { KEY_ROWID, KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION },
KEY_CODE + " LIKE ? OR " + KEY_NAME + " LIKE ?",
new String[] { "%"+inputText+"%", "%"+inputText+"%", },
null, null, null, null);
Please note that you should always use parameters (?) for string values to avoid formatting problems and SQL injection attacks.

Related

building a logical condition query with else and if android sqlite

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.

Filter items in database by comparing more than one colunm?

I want filter items by KEY_PRODUCT_NAME and also KEY_PRODUCT_CATEGORY. How can i do this?
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_PRODUCT_NAME, KEY_PRODUCT_CATEGORY},
KEY_PRODUCT_NAME+ " like '%" + inputText + "%'", null,
null, null, null, null);
You can't use % in your selection directly and also should use AND keyword to filter based on two or more conditions
change your code as follow:
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_PRODUCT_NAME, KEY_PRODUCT_CATEGORY},
KEY_PRODUCT_NAME+ " like ? AND " + KEY_PRODUCT_CATEGORY + " LIKE = ?", new String[] { "%" + inputText + "%", "%"+ category + "%"} ,
null, null, null, null);
This is selection KEY_PRODUCT_NAME+ " like ? AND " + KEY_PRODUCT_CATEGORY + " LIKE = ?" and selectionArgs new String[] { "%" + inputText + "%", "%"+ category + "%"}

more than one selection arguments in SQLite for android

how can I add more than one selection argument?
for example this is one argument selection
inputNumber + "= ?"
new String[]{String.valueOf(numberToCheck)}
full cursor code
Cursor cursor = sqLiteDatabase.query(INSPECTION_PLAN_TRANSACTION,
projection, inputNumber + "= ?", new String[]{String.valueOf(numberToCheck)},
null, null, null, null);
how can i add another condition to the sql statement? like for example
input_number = numberToCheck AND name = "XXX"
input_number is an integer column and name is a column of Strings
sqLiteDatabase.query(INSPECTION_PLAN_TRANSACTION,
projection, input_number + "= ? AND "+ name"= ?", new String[]{String.valueOf(numberToCheck), "XXX"},
null, null, null, null);
Try this:
Cursor cursor = sqLiteDatabase.query(INSPECTION_PLAN_TRANSACTION,
projection, inputNumber + "= ? AND "+name+ " = ?", new String[]{String.valueOf(numberToCheck,nametocheck)},
databse.query(String dbName, String[] columnNames, String whereClause, String[] selectionArgs, String[] groupBy, String[] having, String[] orderBy)
Cursor cursor = database.query(YOUR_TABLE_NAME ,
new String[] {NAME_OF_COLUMNS_WHICH_IS_TO_BE_SELECT} ,
input number + "='" + numberToCheck + "' AND " +
name + "='XXX'",
null, null, null, null);

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.

Android SQLiteDatabase query with LIKE

I have 3 names, Allakhazam, Beatbox and Cunning in my NAMES Table.
public Cursor fetchNamesByConstraint(String filter) {
mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,
KEY_NAME }, KEY_NAME + " LIKE ?",
new String[] { filter }, null, null, null,
null);
return mCursor;
}
I call the function with "A" as the filter, but my cursor is returning a 0 count when it should at least return me a 1. Anyone can see what's wrong with the code?
this statement will return all the records whose keyname equals string specified by string, if you use wild card, then you can get desired results. Like:
mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,
KEY_NAME }, KEY_NAME + " LIKE ?",
new String[] { filter+"%" }, null, null, null,
null);
Will Lists all the records starting with word in filter.
mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,
KEY_NAME }, KEY_NAME + " LIKE ?",
new String[] {"%"+ filter+ "%" }, null, null, null,
null);
Will Lists all the records containing word in filter.
public java.util.Vector<Products> getsearch(String subcategory,String searchby)
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor = db.query(
TABLE_PRODUCTS,
new String[] { SUBCATEGORY, MAIN_CATEGORY, PRODUCT_ID, PRODUCT_NAME, BRAND, PACKAGE_SIZE, PRICE },
SUBCATEGORY + " LIKE '%" + subcategory + "%'",
null, null, null, null, null);
}
Can you try this.....code.......
public static final String KEY_ROWID="row";
public static final String KEY_NAME="name";
public Cursor fetchNamesByConstraint(String filter) {
Cursor cursor=mDb.query(true, DATABASE_NAMES_TABLE, null,"row LIKE '%"+filter+"%' or name LIKE '%"+filter+"%'",null, null, null, null);
}
You should give the filter with wildcards ;)
"A%"
You can use Raw Queries.
public Cursor fetchNamesByConstraint(String filter) {
String query = "SELECT * FROM " + TABLE_NAME + " WHERE " +
COLUMN_NAME + " LIKE '%" + filter + "%'" ;
SQLiteDatabase db = this.getReadableDatabase();
cursor = db.rawQuery(query, null);
return mCursor;
}
public Cursor fetchNamesByConstraint(String filter) {
mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,
KEY_NAME }, KEY_NAME + " LIKE ?",
new String[] {"%"+ filter +"%"}, null, null, null,
null);
return mCursor;
}
The rest is up to our desing, cursor is commanly same
public ArrayList<AboneDataList> getAllPasssiveAboneByDate(String mDate) {
ArrayList<AboneDataList> foodList = new ArrayList<>();
AboneDataList food;
SQLiteDatabase database = dbHelper.getReadableDatabase();
final String kolonlar[] = {DBHelper.COLUMN_A_ID,
DBHelper.COLUMN_A_ABONE_NO,
DBHelper.COLUMN_A_NAME,
DBHelper.COLUMN_A_IS_STUFF,
DBHelper.COLUMN_A_COUNTRY,
DBHelper.COLUMN_A_CITY,
DBHelper.COLUMN_A_TOWN,
DBHelper.COLUMN_A_ADDRESS,
DBHelper.COLUMN_A_PHONE,
DBHelper.COLUMN_A_MOBILE,
DBHelper.COLUMN_A_DATE_START,
DBHelper.COLUMN_A_DATE_END,
DBHelper.COLUMN_A_COUNT,
DBHelper.COLUMN_A_IS_ACTIVE,
DBHelper.COLUMN_A_CARGO,
DBHelper.COLUMN_A_YURT_ID,
DBHelper.COLUMN_A_JOURNAL,
DBHelper.COLUMN_A_MAIL,
DBHelper.COLUMN_A_BUSINESS,
DBHelper.COLUMN_A_BIRTH,
DBHelper.COLUMN_A_GENDER,
DBHelper.COLUMN_A_ABONE_TYPE,
DBHelper.COLUMN_A_MEDENI_TYPE};
//String whereClause = DBHelper.COLUMN_A_JOURNAL + " = ? AND " + DBHelper.COLUMN_A_IS_ACTIVE + " = ? ";
//final String whereArgs[] = {String.valueOf(mJournal),isActive};
//Cursor cursor = database.query(DBHelper.TABLE_ABONE, kolonlar, whereClause, whereArgs,
// null, null, DBHelper.COLUMN_A_ID + " ASC");
Cursor cursor = database.rawQuery("select * from " + DBHelper.TABLE_ABONE + " WHERE "
+ DBHelper.COLUMN_A_DATE_END + " LIKE '%" + mDate + "%'", null);
//Cursor cursor = database.query(DBHelper.TABLE_ABONE, kolonlar, null, null, null, null, DBHelper.COLUMN_A_NAME + " ASC");
while (cursor.moveToNext()) {
food = new AboneDataList();
food.setId(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_A_ID)));
food.setAbone_no(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_A_ABONE_NO)));
food.setName(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_NAME)));
food.setIs_stuff(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_IS_STUFF)));
food.setCountry(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_COUNTRY)));
food.setCity(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_CITY)));
food.setTown(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_TOWN)));
food.setAddress(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_ADDRESS)));
food.setPhone(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_PHONE)));
food.setMobile(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_MOBILE)));
food.setDate_start(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_DATE_START)));
food.setDate_end(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_DATE_END)));
food.setCount(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_COUNT)));
food.setIs_active(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_IS_ACTIVE)));
food.setCargo(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_CARGO)));
food.setYurt_id(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_A_YURT_ID)));
food.setJournal(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_JOURNAL)));
food.setMail(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_MAIL)));
food.setBusiness(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_BUSINESS)));
food.setBirth(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_BIRTH)));
food.setGender(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_GENDER)));
food.setAbone_type(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_ABONE_TYPE)));
food.setMedeni_hal(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_MEDENI_TYPE)));
foodList.add(food);
}
database.close();
cursor.close();
return foodList;
}

Categories

Resources