usage of select statement in sqlite - android

Cursor mCursor = mDb.query(true, CUSTOMER_TABLE_NAME,
new String[] {GENERIC_ID_KEY, ADDRESS_KEY, PHONE_KEY, EMAIL_KEY,CUSTOMER_NAME_KEY},
GENERIC_ID_KEY + "=" + customerDbId, null,
null, null, null, null);
How should I modify this command if GENERIC_ID_KEY and customerDbId is string.
I tried using like in b/w but still negative results, n error is thrown.

if customerDbId is a string type then place it in between quote
as
GENERIC_ID_KEY + "= '" + customerDbId + "'"

It's a cursor problem. Be sure curser is the first state before selection.

Related

how to query sqlite when there are more than one condition that needs to be met

I am using a content provider and need to query a table where one column of the row = thing1 and another column of that same row = thing2.
i know you can query a table for just one condition like:
Cursor c = contentResolver.query(content_uri, projection, variable + "='" + thing1 + "'", null, null);
now how can i query for two conditions? any help would be appreciated.
This should work:
Cursor c = contentResolver.query(content_uri, projection, "col1=? AND col2=?", args, null);
where args is a String[] of values to substitute in for the ? in the query.
Try this
Cursor c = contentResolver.query(content_uri, projection, variable + "='" + thing1 + "' AND "+variable2 + "='"+thing2+"'" , null, null);

query method in SQLiteDatabase

I want to know how to use query method that have where clause with like property.
basically what i want is to select all the columns WHERE C_NAME column is LIKE keyWord.
I've tried this, but it doesn't work:
cursor = db.query(TABLE, null, C_NAME, new String[] {"like '"+keyWord+"'"}, null, null, null);
Look at the docs. They say:
selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.
so try this:
cursor = db.query(TABLE, C_NAME, new String[] {C_NAME + " like '" + keyWord + "'"}, null, null, null);
Also see this answer as well to see some examples on how to use the selectionArgs (4th argument above). That is where keyWord would have to go.
this query work perfect for me
Cursor cursor = db.query(true, TABLE_NAME, new String[]{ COLUMNS }, ROW + " LIKE ?", new > String[] { "%" + name + "%" }, null, null, null, null);

android sql - how do you order your sql query by multiple columns

I am currently working on an Android project in Eclipse and i am having problems with my SQL query.
I am trying to order the query by more than two columns, currently i am doing it by KEY_DAY_ID but i want to also do it by KEY_START_TIME, but i can't get it to work
my query currently looks like this:
Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE,
KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
null, null, null, null, KEY_DAY_ID + " ASC");
Please let me know your thoughts. Thank you in advance!
The last parameter in db.query() method is the order by clause (without the "order by"). All you need to do is separate both columns by a ",". So it would look like:
Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE,
KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
null, null, null, null, KEY_DAY_ID + " ASC, " + KEY_START_TIME + " ASC");
This works for me
SQLiteCursor cursor = (SQLiteCursor) db.query(DbHelper.TIMES, colmn, null, null, null, null, DbHelper.TABLE_DAY + " ASC, " + DbHelper.TABLE_LECTURE_NO + " ASC",null);
Also you can do it in select line like this:
Cursor data = ddbb.rawQuery("SELECT * FROM vacations ORDER BY NAME ,MONTH , date ",null);
in previous code the first probability for the first column "NAME" then will start arrange by the Second probability "MONTH" then the third "date".....
which mean working in series
Or:
Cursor data = ddbb.rawQuery("select * from vacations where NAME = ? ORDER BY MONTH AND date ",new String[]{ns});
in previous code by using "AND" the two conditions are working together in parallel

how to use db.query() when i have many conditions in where clause?? #android

i have a table with columns ID,SUBJECT,BRANCH
i have select the rows which satisfies the follwing conditions
id=rollno
branch=cse
subject=any subject
db.query(DATABASE_TABLE, new String[] {
ID,SUBJECT,BRANCH},
where clause,
null,
null,
null,
null);
how to write where clause here to satisfy my conditions?
You can use db.rawQuery methos also
Cursor cursor= db.rawQuery("select * from <table_name> where id='1' and
branch='" + branchName + "' and subject='" + subjectName+ "'");
add Where clauses with " AND ".. Pretty simple.. Each condition is seperated by the same..
here is an example
String addrWhere = ContactsContract.Data.CONTACT_ID+" = ? AND "+ContactsContract.Data.MIMETYPE+" = ?"; //this is where clause and below is corresponding selection args..
String[] addrWhereParams = new String[]{data.getContac_ID(),
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};

Retrieving contact info in Android fails with exception

ALL,
I am trying to execute following piece of code:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query( ContactContract.Contacts.CONTENT_URI, null, "ContactsContract.Contacts.DISPLAY_NAME LIKE '" + name + "'", null, null );
However when running this code, I am getting SQLite exception: "Don't know such file ContactsContract.Contacts.DISPLAY_NAME:, while compiling "....."".
The problem is that I don't know in advance what will "name" contain, hence using "LIKE" clause.
Is there a better way to perform such operation? Or I am just doing it incorrectly?
Thank you in advance for any help.
Or I am just doing it incorrectly?
ContactsContract.Contacts.DISPLAY_NAME is a Java construct. Use:
Cursor cur = cr.query( ContactContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.DISPLAY_NAME + " LIKE '" + name + "'", null, null );
Or, use positional parameters:
Cursor cur = cr.query( ContactContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?", args, null );
where args is a one-element string array containing your name.

Categories

Resources