I need to compare the last 10 digit of the phone numbers in such code:
String selectionClause = ContactsContract.CommonDataKinds.Phone.NUMBER+ " = ?";
String[] selectionClauseArgs = { callerId };
Cursor people = resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projections,
selectionClause, selectionClauseArgs, null);
So the selectionClause should be like
String selectionClauseArgs = "substr("+CommonDataKinds.Phone.NUMBER+",-1,10) = ?";
But I am not sure if I can use such SQLLite queries when querying ContentProviders.
Try this solution, I have tested it myself and should be work.
//substr(string,-10, 10) take last 10 characters.
String selectionClause = "substr(" + ContactsContract.CommonDataKinds.Phone.NUMBER + ",-10, 10) =?";
String[] selectionClauseArgs = { callerId };
Cursor people = resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projections,
selectionClause, selectionClauseArgs, null);
and also you can use substr(COLUMN_NAME,-10) to take the last 10 characters.
note : For function substr(x,y,z), y is the beginning character, if y is negative, then it count from the right of the x. and z is the number of characters taken begin from the y position.
Related
I use below code to get number from database from given number, for that I fired below query to get number but it does not return number. Is there wrong something in my query? please help me to resolved this issue.
I need to use LIKE function because some number stored with special char and whitespace.
contactNumber = contactNumber.replaceAll("[-+.^:,]", "");
contactNumber="%"+contactNumber+"%";
String strEmail = "";
String name = "";
Cursor cursor = ctx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?", new String[]{contactNumber}, null);
This is the way to use LIKE when using ContentProviders:
getContentResolver().query(Phone.CONTENT_URI, null, Phone.NUMBER + " LIKE ?", new String[] { "%" + contactNumber + "%" }, null);
However, the best way to search by phone number is to use the dedicated PhoneLookup API:
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
getContentResolver().query(uri, new String[]{PhoneLookup.CONTACT_ID, PhoneLookup.DISPLAY_NAME}, null, null, null);
It's both optimised to return a result faster, and handles phone numbers with or without country-codes.
I have to read contacts from device which are having valid phone numbers. For that right now I'm using following query.
final Uri contentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
final String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '" + ("1") + "' AND LENGTH(" + ContactsContract.CommonDataKinds.Phone.NUMBER + ") >= 10";
final String[] projection = null;
final String[] selectionArgs = null;
final String sortOrder = "upper(" + ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID + ") ASC";
Cursor phones = mContentResolver.query(contentUri, projection, selection, selectionArgs, sortOrder);
As you can see in selection , im querying for entries with HAS_PHONE_NUMBER = 1 and length of phone number >= 10 , to avoid maximum junk contacts while retrieving from itself. It works fine , but the problem is , the saved phone numbers might have special characters like - , ( , )or a space etc.
Like +9191-91-22-22-55 , +9191-91-(22 22-55) . I need to get this string without non-digits and without country code (last 10 numbers).
For example : +9190-91-22-22-55 as 9091222255
is there any way to retrieve a string field as formatted like this in SQLite? Or is there any effective way to fulfill my requirement? Thanks in advance..
I want to use this:
return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_LEVEL }, KEY_LEVEL + ">= 3 AND " + KEY_LEVEL + " < 5", null, null, null, null);
But instead of 5 I want it to check the next column where it finds greater than or equal to three.
So when I give an input of say 5, I want it to check for greater than or equal to 5 in column 3 but less than the value in the next column, column 4.
Original code taken from Sqlite query check - less than and greater than.
Just do this :
String[] selectionArgs = { "5", "5" };
String[] columns = { KEY_ROWID, KEY_COLUMN3, KEY_COLUMN4 };
String selection = "KEY_COLUMN3 >= ? AND KEY_COLUMN4 < ?";
return mDb.query(DATABASE_TABLE, columns, selection, selectionArgs, null, null, null);
For me just passing numerics in String format like #buzeeg offered didn't work.
PROBLEM: My task was to perform UPDATE and in my case it should not have succeed, but as you may guess, when we use 1 as query arg, '1'>2 means true:
UPDATE table SET field = 1 WHERE ? > 2
SOLUTION: CAST helped me to solve it:
SELECT * FROM smth WHERE CAST(? as integer) > 2
I'm trying to use the following:
String dbquery = "Select * From stops where stopname LIKE '%?%' LIMIT 100";
String[] uinputarray = {uinput};
Cursor c = sDB.rawQuery(dbquery, uinputarray);
This consistently crashes.
Curiously, if I use the old test code of:
Cursor c = sDB.rawQuery("Select * From stops where stopname LIKE '%" + uinput + "%' LIMIT 100", null);
It works flawlessly.
I've been reading up on selectionArgs and I honestly can't see anything wrong with what I've done.
What am I doing wrong?
Thanks guys
You have to append the % to the selectionArgs itself:
selectionArgs = new String[] { searchString + "%" };
Cursor c = db.rawQuery("SELECT column FROM table WHERE column=?", selectionArgs);
Note: Accordingly % and _ in the searchString string still work as wildcards!
I am working on a database with sqllite in an android app
I want to retrieve sm data using a like clause
ex:
Cursor c = myDB.query(MY_DATABASE_TABLE, null, " SongName LIKE '%"+"=?"+"%'" ,
new String[]{match_str}, null, null,"SongHit DESC");
It should give all SongName starting with match_str but its not working.Why?
This:
" SongName LIKE '%"+"=?"+"%'"
Will end up looking like this when the SQL interpreter sees it:
" SongName LIKE '%=?%'"
And that will match any SongName that contains a literal "=?" and I don't think that's anything like what you want.
A % matches any sequence of characters in an SQL LIKE, it is essentially the same as .* in a regular expression; so, if you want to match at the beginning then you don't want a leading %. Also, your ? placeholder needs to be outside the single quotes or it will be interpreted as a literal question mark rather than a placeholder.
You want something more like this:
String[] a = new String[1];
a[0] = match_str + '%';
Cursor c = myDB.rawQuery("SELECT * FROM songs WHERE SongName LIKE ?", a);
If you wanted to be really strict you'd also have to escape % and _ symbols inside match_str as well but that's left as an exercise for the reader.
Try this:
String[] args = new String[1];
args[0] = "%"+song+"%";
Cursor friendLike = db.rawQuery("SELECT * FROM songs WHERE SongName like ?", args);
No need for the equal sign and you should be able to include the ? right in the string without the need for the + operator. The where clause should just be:
"SongName LIKE '%?%'"
and if mu is too short is correct and you only want starting with...
"SongName LIKE '?%'"
hi Are you getting any exception while executing the above query. Try by removing the "=" symbol infront of the question mark in the like condition. Else try using
db.rawQuery(sql, selectionArgs)
This works perfectly for me...
"Songname LIKE ? "...
cursor = database.rawQuery(query, new String[]{"%" + searchTerm + "%"});
If you wish to use query() instead of rawQuery(), you can do it like so:
// searchString is the string to search for
final String whereClause = "MyColumnName" + " like ?";
String[] whereArgs = new String[]{"%" + searchString + "%"};
Cursor cursor = sqLiteDatabase.query("MyTableName",
null, // columns
whereClause, // selection
whereArgs, // selectionArgs
null, // groupBy
null, // having
null, // orderBy
null); // limit
a simple way is to use the concatenate operator ||
"SongName LIKE '%'||?||'%'"
so there is no need to edit the match_str.
Cursor c = myDB.query(MY_DATABASE_TABLE, null, " SongName LIKE '%'||?||'%'" ,
new String[]{match_str}, null, null,"SongHit DESC");
from https://stackoverflow.com/a/46420813/908821