I am creating a messaging application in android and i want to get text messages from unique contact numbers only.
I tried following code but it is not working.
String[] projection = new String[] { "_id","DISTINCT "+ContactsContract.PhoneLookup.NUMBER, "person", "body", "date", "type" };
Cursor cursor = getContentResolver().query(uri, projection,null, null, null);
It throwing error here.
Caused by: android.database.sqlite.SQLiteException: near "DISTINCT": syntax error: , while compiling: SELECT _id, DISTINCT number, person, body, date, type FROM sms ORDER BY date DESC
Can Some one please crack this out for me.
You need to change your query. The DISTINCT keyword only works with all rows. What you need is a Group By section in your query.
You could change your query to:
String[] projection = new String[] { "_id", ContactsContract.PhoneLookup.NUMBER, "person", "body", "date", "type" };
String selection = GROUP BY " + ContactsContract.PhoneLookup.NUMBER;
Cursor cursor = getContentResolver().query(uri, projection, selection, null, null);
I've not tested this yet, so there might be syntax errors.
Related
I want to add functionality of contains() method in where clause. Right now I was doing something like "address = ?" and providing address in arguments but now I want to use a logic which works as address.contains(?) and then providing the address in arguments. How can I implement it?
Till now my code is and need to know the require modification.
Uri mSmsinboxQueryUri = Uri.parse("content://sms");
String[] projection = new String[] { "_id", "thread_id", "address",
"person", "date", "body", "type" };
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
projection, "address = ?", new String[]{addressToBeSearched}, null);
Try this
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
projection, "address LIKE ?", new String[]{addressToBeSearched + "%" }, null);
Is there a way to tell if a phone number has an sms(s) in android ??
I want to know if there is a way to tell using the sms content porvider I know its not documented and not recommanded to use but is there a way to tell ??
what i have tried .
this statement will get all the inbox sms(s)
cursor = getContentResolver().query(
Uri.parse("content://sms/inbox"), null, null, null,
null);
I cant use the where clause like this
cursor = getContentResolver().query(
Uri.parse("content://sms/inbox"), "address =?", new String[]{"001435436654747"}, null,
null);
because the number may be stored without the global code the "001" or such .
is there a work around for this ??
Hi you can use below code to do this , I use this in my application to listen incoming sms to reply automaticaly.
Uri myMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(myMessage, new String[] {
"_id", "address", "date", "body",
"read" }, null, null, null);
System.out.println(Number);
while (c.moveToNext()) {
Number = c.getString(c.getColumnIndexOrThrow("address")).toString();
if (Number.equals("001435436654747")) {
// DO YOUR STUFF
}
}
I want to display a list of the contact names from the SMS inbox and outbox (like in the native messaging app). I have come up with the following code:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Uri messagesUri = Uri.parse("content://sms/");
Cursor cursor = getContentResolver().query(messagesUri,new String[] { "_id", "thread_id", "address", "person", "date", "body", "type" }, null, null, null);
startManagingCursor(cursor);
String[] columns = new String[] { "address", "person", "date", "body", "type" };
String sms = "";
if (cursor.getCount() > 0) {
while (cursor.moveToNext()){
String address = cursor.getString(cursor.getColumnIndex(columns[0]));
sms += address + " ";
String contact=address;
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cs= getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
startManagingCursor(cs);
if(cs.getCount()>0)
{
cs.moveToFirst();
contact=cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
listItems.add(contact);
}
}
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
This works when I run the application in an emulator, but when I try to run it on a phone I get a NullPointerException. If I double-click on the error message in the LogCat the following line gets highlighted:
if(cs.getCount()>0)
What is the problem here?
From the documentation for ContentResolver.query(...)
A Cursor object, which is positioned before the first entry, or null
You must always check for null when using a ContentResolver to query for data.
Also from the same documentation.
Use question mark parameter markers such as 'phone=?' instead of explicit values in the selection parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.
So instead of:
Cursor cs= getContentResolver().query(uri, new String[ {PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
Do:
Cursor cs= getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"=?",new String[]{address},null);
My first guess would be that cs is null. As to why it is null, I suppose that the carrier/manufacturer could have changed how this is implemented on your device. Without knowing too much about how these systems work, it looks like you are querying against some kind of database and then using a cursor to iterate over the data. If the representation has been changed, the cursor will break. My suggestion would be to see if there is some other solution that is more Google/API driven (as opposed to reading the raw data).
I have a question regarding how to use a WHERE clause when querying a sql database in Android. I need to return specific records from my database where the value of DURATION is greater than 3.
It works fine when I have the WHERE clause for checking equals.
Example
Cursor resultOfFilterQuery = db.query(myTable, new String[] {call_cost, call_type,
date,DURATION , phone_number }, phone_number= , new String[]{"9456788909"}, null, null, null);
Please let me know how to check for greater than
How should the query statement look?
Cursor resultOfFilterQuery = db.query(myTable, new String[] {call_cost, call_type,
date,DURATION , phone_number }, DURATION> , new String[]{3}, null, null, null);
Don't know how your first code snippet work with syntax errors, but this can helps:
Cursor resultOfFilterQuery = db.query(myTable,
new String[] {call_cost, call_type, date, DURATION, phone_number },
DURATION + "> ?", new String[]{"3"}, null, null, null);
I am writing an android application that is trying to pull data from a database based on two separate criteria.
The fields of workout and exercise are both strings in the database. I want to return a cursor with only those rows that satisfy BOTH criteria. Oh and I would also like it to be sorted in date order...
public Cursor graphQuery(String exercise, String workout) {
Cursor cursor = mDb.query(DATABASE_TABLE, new String [] {KEY_DATE, KEY_REPS,
KEY_REPS_FEEL, KEY_WEIGHT, KEY_WEIGHT_FEEL}, "KEY_WORKOUT=" + workout + "AND" +
"KEY_EXERCISE=" + exercise, null , null, null, KEY_DATE);
return cursor;
}
I am a new android coder and would appreciate the help!
Use selection and selectionArgs:
public Cursor graphQuery(String exercise, String workout) {
Cursor cursor = mDb.query(DATABASE_TABLE, new String [] {KEY_DATE, KEY_REPS,
KEY_REPS_FEEL, KEY_WEIGHT, KEY_WEIGHT_FEEL}, "KEY_WORKOUT = ? AND KEY_EXERCISE = ?",
new String[] { workout, exercise },
null,
null,
KEY_DATE);
return cursor;
}
Notice how the selection String has ?s inplace of actual values and the actual values are passed in as a String[] and in the selectionArgs paramter. SQLite will replace those ?s with the values from the String[] selectionArgs.
Short answer: There are spaces missing around "AND" --> " AND ".:
Long answer: Please use parameter markers - you will see missing spaces immediately then:
Cursor cursor = mDb.query(DATABASE_TABLE, new String [] { KEY_DATE, KEY_REPS,
KEY_REPS_FEEL, KEY_WEIGHT, KEY_WEIGHT_FEEL }, "KEY_WORKOUT=? and KEY_EXERCISE=?", new String[] { workout, exercise }, null, null, KEY_DATE);