more than one selection arguments in SQLite for android - 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);

Related

Android Search Query and Sort in order

Anyone has an idea how this is done? I have a regular search query below that displays matching data in a listview, i tried putting COLUMN NAME + " ASC" at the order part, but i got errors.
public Cursor getSearch(String query)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, KEY_NAME + " LIKE ?",
new String[] {"%"+ query+ "%" }, null, null, null,
null);
if(c != null)
{
c.moveToFirst();
}
return c;
}
The 'Order by' clause should be the 8th argument in the query call:
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, KEY_NAME + " LIKE ?",
new String[] {"%"+ query+ "%" }, null, null, COLUMN_NAME + " ASC", null);
See: Documentation

Query last N rows based on ID in SQLite

I have a SQLite database in which I am trying to read the last 8 rows in database using ID.
I am having trouble with the query. Can't seem to get it right.
public String lastEightRows(){
String[] columns = new String[] {KEY_ROWID, KEY_COLORS};
String result30 = " ";
String where = "ORDER BY" + KEY_ROWID + "DESC LIMIT" + 8;
//This is where I am not able to get the query right
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, where, null, null, null, null);
int iRow = c.getColumnIndex(KEY_ROWID);
int iColors = c.getColumnIndex(KEY_COLORS);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result30 = result30 + c.getString(iRow) + " " + c.getString(iColors) + " " + "\n";
}
return result30;
}
I got this error message
03-26 22:58:27.420: E/AndroidRuntime(1368): android.database.sqlite.SQLiteException: near "DESC": syntax error (code 1): , while compiling: SELECT _id, persons_colors FROM personsTable WHERE _id DESC LIMIT 8
From Android SQLiteDatabase API:
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Query the given table, returning a Cursor over the result set.
Try this code instead
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, KEY_ROWID + " DESC", "8");
sqlitedatabase.query() defined query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
So, try change this.
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, KEY_ROWID + " DESC", "8");

SQLiteDatabase - How to use where clause?

public Cursor fetchTimetable() {
return mDb.query(DATABASE_TABLE_TIMETABLE, new String[] {TIMETABLE_ROWID, TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_ROOM, TIMETABLE_LECTURER, TIMETABLE_TIME}, null, null, null, null, null);
}
For example if i wanted TIMETABLE_MODULECODE = 123. How would i do this, i have read its the first null. I have tried this below but still doesnt work
public Cursor fetchTimetable() {
String a = "TIMETABLE_CODE = ADD";
return mDb.query(DATABASE_TABLE_TIMETABLE, new String[] {TIMETABLE_ROWID, TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_ROOM, TIMETABLE_LECTURER, TIMETABLE_TIME}, a, null, null, null, null);
}
My working example with where args (it's more clear with using it):
String [] settingsProjection = {
DBContract.Settings._ID,
DBContract.Settings.COLUMN_NAME_USER_ID,
DBContract.Settings.COLUMN_NAME_AUTO_LOGIN
};
String whereClause = DBContract.Settings.COLUMN_NAME_USER_ID+"=?";
String [] whereArgs = {userId.toString()};
Cursor c = db.query(
DBContract.Settings.TABLE_NAME,
settingsProjection,
whereClause,
whereArgs,
null,
null,
null
);
db.query
(
TABLE_NAME,
new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO },
TABLE_ROW_ID + "=" + rowID,
null, null, null, null, null
);
TABLE_ROW_ID + "=" + rowID, here "=" is the where clause
What exactly does "still doesn't work" mean?
The following code should work just fine:
public Cursor fetchTimetable() {
String[] columnNames = new String[] {TIMETABLE_ROWID, TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_ROOM, TIMETABLE_LECTURER, TIMETABLE_TIME};
String whereClause = "TIMETABLE_MODULECODE=123";
return mDb.query(DATABASE_TABLE_TIMETABLE, columnNames, whereClause, null, null, null, null);
}
the right syntax is
String[] projection= {column names};
String[] where={"values for where clause"}; //this must be array
public Cursor fetchTimetable() {
return mDb.query(TABLE_NAME, projrction, "columname"+"=?", where, null, null, null);
}
The simple way:
return mDb.rawQuery("SELECT * FROM myTable WHERE column1 = "+ someValue, null);
You can use Use this query:
SQLiteDatabase db = this.getReadableDatabase();
//Cursor cursorr = db.rawQuery(Query, null);
Cursor cursorr = db.rawQuery("select * from " + DATABASE_TABLE_EHS + " where " + TASK_ID + "='" + taskid + "'" , null);
if (cursorr.moveToFirst())
{
do {
// your code like get columns
}
while (cursorr.moveToNext());
}
}
If you're checking against a string value you'll want to wrap the string in quotes like below:
dbHelper.COL_EMAIL +"='"+givenEmail+"'",

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

Retrieving group of particular contact

I want to retrieve the contact details along with the group which it belongs to. I got the code to list all the contact groups in the phone.
Cursor groupC = getContentResolver().query(
ContactsContract.Groups.CONTENT_URI, null, null, null, null);
while (groupC.moveToNext()) {
String groupid =
groupC.getString(groupC.getColumnIndex(ContactsContract.Groups._ID));
Log.e("myTag", groupid);
String grouptitle =
groupC .getString(groupC.getColumnIndex(ContactsContract.Groups.TITLE));
Log.e("myTag", grouptitle);
}
groupC.close();
Then I tried to query for a particular contact by using its id but it always shows There is no such column....
Cursor groupC = getContentResolver().query(
ContactsContract.Groups.CONTENT_URI,
null,
ContactsContract.Contacts._ID+"= ?",
new String[]{id},
null);
where id is
Cursor cur = cr.query(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null);
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
How to query the group using a particular contact id?
I found the answer.we should pass the raw contact-id and the correct mime type.
String where = ContactsContract.Data.RAW_CONTACT_ID
+ "="
+ Integer.parseInt(id)
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
+ "'";
Cursor cursor = ctx
.getContentResolver()
.query(ContactsContract.Data.CONTENT_URI, null, where, null,
null);
startManagingCursor(cursor);
Log.e("Count is:", ""+ cursor.getCount());
while (cursor.moveToNext()) {
groupid = cursor
.getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
Log.e("groupid", groupid);
builder.append(groupid);
}String where = ContactsContract.Data.RAW_CONTACT_ID
+ "="
+ Integer.parseInt(id)
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
+ "'";
Cursor cursor = ctx
.getContentResolver()
.query(ContactsContract.Data.CONTENT_URI, null, where, null,
null);
startManagingCursor(cursor);
Log.e("Count is:", ""+ cursor.getCount());
while (cursor.moveToNext()) {
groupid = cursor
.getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
Log.e("groupid", groupid);
break;
}
A contact may be in more than one group,here it retrivr its first group pnly.
I think this may be useful to somebody...

Categories

Resources