I need to return the ID value from the database where dan == table_column_one and where vrijeme == table_column_two. I don't know how to do this.
public static int returnID(String dan, int vrijeme){
Cursor cursor;
int IDRQ;
cursor = db.query
(
TABLE_NAME,
new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO },
new String[] {TABLE_COLUMN_ONE + " like " + "'%" + dan + "%'", TABLE_COLUMN_TWO + " like " + "'%" + vrijeme + "%'"}, null, null, null, null
);
cursor.moveToFirst();
IDRQ = cursor.getInt(0);
return IDRQ;
} );
This is how I tried, but of course, its wrong.
In SQLiteDatabase.query() the selection comes in two parts. The where clause (a String) and the whereArgs (an array of String).
To add more than one condition to the where clause you can use AND or OR, just like && or || in Java.
A question mark in the where clause is bound to one of the Strings in the whereArgs array.
cursor = db.query(TABLE_NAME, new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO },
TABLE_COLUMN_ONE + " LIKE ? AND " + TABLE_COLUMN_TWO + " LIKE ?",
new String[] {"%" + dan + "%", "%" + vrijeme + "%"},
null, null, null, null);
Related
I saved data on sqlite db with year and month, now want to retrieve data based on year and month but object is always null.
Here is my code
public List<AddIncomeModel> fetch(String year,String month) {
database = this.getReadableDatabase();
// Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null);
Cursor cursor=database.rawQuery("SELECT * FROM " + TABLE_NAME+" WHERE " +YEAR +" = "+year+" AND " + MONTH + " = "+month,null);
List<AddIncomeModel> contacts = new ArrayList<AddIncomeModel>();
AddIncomeModel contactModel;
if (cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToNext();
contactModel = new AddIncomeModel();
contactModel.setId(cursor.getInt(0));
contactModel.setIncome_source(cursor.getString(1));
contactModel.setDescription(cursor.getString(2));
contactModel.setAmount(cursor.getString(3));
contacts.add(contactModel);
}
}
cursor.close();
database.close();
return contacts;
}
Please tell me where I am going wrong.
Thanks
In SQLiteDatabase.query() the selection comes in two parts. The where clause (a String) and the whereArgs (an array of String).
To add more than one condition to the where clause you can use AND or OR, just like && or || in Java.
A question mark in the where clause is bound to one of the Strings in the whereArgs array.
Try Like This
cursor = db.query(TABLE_NAME, new String[] { ID, YEAR , MONTH},
YEAR + " LIKE ? AND " + MONTH+ " LIKE ?",
new String[] {"%" + dan + "%", "%" + vrijeme + "%"},
null, null, null, null);
I want to query on contacts by phone number and contact name in same time with "LIKE" operator, here is my code :
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1 AND (" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE '" + query + "%' OR " + ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '%" + query + "%' OR " + ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '%" + query + "%' ) ", null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
return cursor;
But my code does not work, it's crashes and android says "data4" and "data1" column does not exist.
Use below code to find contact by name
public String findByName(Context context , String name) {
String result= null;
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'%" + name +"%'";
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, selection, null, null);
if (c.moveToFirst()) {
result= c.getString(0);
}
c.close();
if(result==null)
result= "This contact is not saved into your device";
return result;
}
Use loader to get fastest results.
public Loader<Cursor> getContactCursor(Context context, Bundle args) {
Uri baseUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String filter = args != null ? args.getString("filter") : null;
if (filter != null && filter.length() > 0) {
return new CursorLoader(context,baseUri, null, "(display_name LIKE ?) OR (data1 LIKE ?)", new String[]{"%" + filter + "%","%" + filter + "%"}, null);
}else{
return new CursorLoader(context,baseUri, projection,null, null, null);
}
}
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 + "%"}
I have a database table with several columns in it. I want to select certain rows by comparing a string entered by a user and select records based on their entry. I have the following code:
SearchbyIngredient.Java
final Button searchbutton1 = (Button) findViewById(R.id.searchbutton1);
searchbutton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Cursor temp = dbIngredientHelper.getDrinks(textView.getText().toString());
String ingred = temp.getString(0);
Context context = getApplicationContext();
iadapter = new IngredientAdapter(temp);
myListView.setAdapter(iadapter);
}
});
}
IngredientHelper.Java
public Cursor getDrinks(String drinkName) throws SQLException{
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(TABLE_NAME2);
String selection = COLUMN_INGRED1 + "= ' " + drinkName + "'";
String[] asColumnsToReturn = new String[] { COLUMN_ID2, COLUMN_DRINKNAME, COLUMN_INGRED1, COLUMN_AMT1, COLUMN_INGRED2, COLUMN_AMT2, COLUMN_INGRED3, COLUMN_AMT3, COLUMN_INGRED4, COLUMN_AMT4 };
Cursor mCursor2 = queryBuilder.query(dbSqlite, asColumnsToReturn, selection, null, null, null, COLUMN_DRINKNAME);
/*Cursor mCursor = dbSqlite.query(true, TABLE_NAME2, new String[]{
COLUMN_DRINKNAME},
COLUMN_INGRED1 + "= ' " + drinkName + "'", null, null, null, null, null);
if (mCursor != null){
mCursor.moveToFirst();
}*/
mCursor2.moveToFirst();
return mCursor2;
}
My question is regarding
String selection = COLUMN_INGRED1 + "= ' " + drinkName + "'";
This code currently works, however it only finds entries with the user's text in COLUMN_INGRED1 (only a single column in the code). How can I format the statement or make a series of statements so that it also looks for the user's text in COLUMN_INGRED2, COLUMN_INGRED3 and COLUMN_INGRED4? Thank you
Standard SQL applies, use an OR:
COLUMN_INGRED1 + "= ' " + drinkName + "' OR " COLUMN_INGRED2 + "= '" +drinkName + "'" ...;
Note that = will do an exact match, you might want to look into the LIKE operator.
Cant anyone tell me why I the flowing query isn't working properly?
It suppose to return true if there are types with same name and different _ID(Key)
//****************************************************************************************************//
// ifExistButMy(String typeName,int ID) FUNCTION: return true if type name exists but mine(ID)
//
//****************************************************************************************************//
public boolean ifExistButMy(String typeName,int ID){
Cursor cur;
SQLiteDatabase db = content.getWritableDatabase();
cur = db.query(TABLE_NAME, null,TYPE_NAME + "='" + typeName + "'"+" AND "+ _ID+ " <> " + ID,null, null, null, null);
return cur.moveToFirst(); //returns false if cur is empty
}
String[] mFields=new String[]{"columnName1","columnName2",....}
cur = db.query(TABLE_NAME, mFields,TYPE_NAME + "='" + typeName + "'"+" AND "+ _ID+ " <> " + ID,null, null, null, null);
your second argument is null rather than having which field to be selected