I have the variable string (user gets to set it via a ListView with tickboxes, so string can contain any number of fruits)
private static final String kernset = "Banana, Peach"
I have the following SQLite Query
Cursor cursor = db.query (TABLE_NAME, new String[] {
WOORD },
"kern IN (\"Banana\", \"Apple\")", null, null, null, null, null);
I want to replace the (\"Banana\", \"Apple\") part of the query with the variable.
How should I do that?
Thanks!
If you want to place a variable into your selection parameter, you can split your string using split method, and then form your selection like
StringBuilder sb = new StringBuilder();
sb.append("kern IN (");
String[] inItems = kernset.split("\\,\\s+");
for (int i=0; i < inItems.length; i++){
if (i > 0)
sb.append(", ");
sb.append("'" + inItems[i] + "'");
}
sb.append(")");
String selection = sb.toString();
however if you wish to use ? in selection and replace it with selectionArgs[] it seems to be imposible since values passed in selectionArgs are surrounded with ' '. You can however form selection with ? instead of actual values (like kern IN (?, ?, ?))and then pass inItems via selectionArgs[]
by just using concatenation like
Cursor cursor = db.query (TABLE_NAME, new String[] {
WOORD },
"kern IN (\""+var1+"\", \""+var2+"\")", null, null, null, null, null);
for unknow no of vars
inString="";
for(int i=0;i<fruits.length();i++)
{
inString=inString+"\""+fruits[i]+"\"";
if(i<(fruits.length()-1))
{
inString=inString+", "
}
}
Cursor cursor = db.query (TABLE_NAME, new String[] { WOORD },
"kern IN ("+inString+")", null, null, null, null, null);
Related
I have a String[] that looks like {1, 2, 3 ..} (a string of IDs).
I want to build a query in Android to obtain all the entries that match the IDs.
Here my code:
Cursor idFoodCursor = getContext().getContentResolver().query(
uriFood,
null,
CookingContract.FoodEntry.COLUMN_NAME + " LIKE ?",
new String[]{selectionArgs},
null
);
if (idFoodCursor.moveToFirst()) {
List<String> ids = new ArrayList<String>();
while (!idFoodCursor.isAfterLast()) {
ids.add(idFoodCursor.getString(idFoodCursor.getColumnIndex(CookingContract.FoodEntry._ID)));
idFoodCursor.moveToNext();
}
idFoodCursor.close();
//Convert the ArrayList in String[]
String[] idSelectionArg = new String[ids.size()];
ids.toArray(idSelectionArg);
return new CursorLoader(
getContext(),
uriFood,
FOOD_COLUMNS,
CookingContract.FoodEntry._ID + " = ?",
idSelectionArg,
sortOrder
);
}
The last query doesn't work because I should add as many "?" as my IDs in the array:
Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 3 because the index is out of range. The statement has 1 parameters.
How can I fix the problem, taking into account what I want to get? (the correspondence of all the ids in the table)
All the over-mentioned code can be replaced with:
return new CursorLoader(
getContext(),
uriFood,
FOOD_COLUMNS,
"_id IN (SELECT _id FROM food WHERE name LIKE ?)",
new String[] {selectionArgs},
sortOrder
);
That does the job I wanted.
public class Constants {
public static final String JOB_STATUS_CANCELLED = "Cancelled";
public static final String JOB_STATUS_COMPLETE = "Complete";
}
selection = JobListContract.JobEntry.COLUMN_NAME_JOB_STATUS + " NOT IN ( ? , ? ) ";
// The spaces matter!!!!
selectionArgs = new String[]{ Constants.JOB_STATUS_COMPLETE, Constants.JOB_STATUS_CANCELLED };
c = db != null ? db.query(
JobListContract.JobEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
JobListContract.JobEntry.COLUMN_NAME_ENTRY_ID + " desc" // The sort order
) : null;
I know how to query for a single selection with the following:
Cursor cursor = database.query(true, TABLE, COLUMNS, "name = ?", new String[]{"Bob"},null,null,null,null);
But suppose I want to make a function as follows:
public Cursor queryNames(String[] names)
where the function returns a cursor where the name = names[0] OR name = names[1] ... etc. So for example, if I called the function queryNames(new String[] {"Alice","Bob","Charlie"}), the function should return a cursor where the name is any of the three (Alice, Bob, or Charlie). How would I write this? Thanks!
Your method might want to look like this:
public Cursor queryNames(String[] names) {
SQLiteDatabase mDb = this.getReadableDatabase();
String whereStatement = "";
for(int i = 0; i < names.length; i++) {
if (i != (names.length - 1))
whereStatement = whereStatement + "name = ? OR "
else
whereStatement = whereStatement + "name = ?"
Cursor cursor = mDb.query(true, TABLE, COLUMNS, whereStatement, names, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
mDb.close();
return cursor;
}
Hope this helps!
try:
Cursor cursor = db.query(true, TABLE, COLUMNS, "name IN (?)", new String[]{" 'moe', 'larry', 'curly'"}, null, null, null, null);
It would probably be best to build the String[] separately than to guess at the number of names.
Enclose the whole thing in double quotes, the individual names in single quotes, comma-separated.
I'm developing an SMS program and I want to get conversations.
I wrote the code below and it works fine, but I wonder if it could be more efficient
This is for geting conversation threads
Uri SMS_INBOX = Uri.parse("content://sms/conversations/");
Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, "date desc");
startManagingCursor(c);
String[] count = new String[c.getCount()];
String[] snippet = new String[c.getCount()];
String[] thread_id = new String[c.getCount()];
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++) {
count[i] = c.getString(c.getColumnIndexOrThrow("msg_count"))
.toString();
thread_id[i] = c.getString(c.getColumnIndexOrThrow("thread_id"))
.toString();
snippet[i] = c.getString(c.getColumnIndexOrThrow("snippet"))
.toString();
//Toast.makeText(getApplicationContext(), count[i] + " - " + thread_id[i]+" - "+snippet[i] , Toast.LENGTH_LONG).show();
c.moveToNext();
}
c.close();
for getting addresses according to conversation thread
for(int ad = 0; ad < thread_id.length ; ad++)
{
Uri uri = Uri.parse("content://sms/inbox");
String where = "thread_id="+thread_id[ad];
Cursor mycursor= getContentResolver().query(uri, null, where ,null,null);
startManagingCursor(mycursor);
String[] number = new String[mycursor.getCount()];
if(mycursor.moveToFirst()){
for(int i=0;i<mycursor.getCount();i++){
number[i]=mycursor.getString(mycursor.getColumnIndexOrThrow("address")).toString();
mycursor.moveToNext();
}
}
mycursor.close();
and finally checking the adresses (if in contact list) and adding to a list
for(int i =0 ; i < numaralar.length ;i++)
{
String a = numaralar[i].substring(0,1);
if(!a.equals("+")){ kisiismi = numaralar[i]; }
ContentResolver localContentResolver = getApplicationContext().getContentResolver();
Cursor contactLookupCursor =
localContentResolver.query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(numaralar[i])),
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
try {
while(contactLookupCursor.moveToNext()){
String contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
kisiismi = contactName;
}
}catch (Exception e) {
kisiismi = numaralar[i].toString();
}
finally {
//Toast.makeText(getApplicationContext(), ad + kisiismi + " " + count[ad], Toast.LENGTH_LONG).show();
myArr.add(kisiismi);
contactLookupCursor.close();
}
}
Are there any way to make this process easier?
Since it is a simple SQLite query and the Contact provider can be accessed in a similar way, you should try to get the job done in SQL by GROUPING via number, timestamp and maybe thrad_id and then matching the results to a query to the contact provider (also via SQLite)
The documentation holds a pretty good description of all available columns.
https://developer.android.com/reference/android/provider/ContactsContract.PhoneLookupColumns.html
From KitKat onwards, we have a specific Uri for this: content://mms-sms/messages/byphone. But before that, this is what we can come up with:
Set<Integer> conversationIds = new HashSet<Integer>();
String numbers = "+xxxxxxxxxx,+yyyyyyyyyy";
final String[] PROJECTION = { Sms._ID, Sms.THREAD_ID };
final String SELECTION = Sms.ADDRESS + " IN (" + numbers.replaceAll("[^,]+", "?") + ")";
final String[] selectionArgs = numbers.split(",");
Cursor cursor = context.getContentResolver().query(Sms.CONTENT_URI, PROJECTION, SELECTION, selectionArgs, null);
int threadColumn = cursor.getColumnIndexOrThrow(Sms.THREAD_ID);
while (cursor.moveToNext())
conversationIds.add(cursor.getInt(threadColumn));
cursor.close();
return conversationIds;
There is no easy way to do the same with MMS messages because they don't keep their address in the database, you have to query each and every one separately. If you need to do it repeatedly, a viable solution is to cache MMS-to-phone number relations in a database of your own.
You can then use the identifiers accumulated in conversationIds to query the individual conversations. Note that if you want to merge different coversations belonging to the same contact, you can reuse the same query selection pattern above with passing in all ids as IN (?,...,?) at once.
I test sql and it works fine in Sqlite Spy:
select ifnull(_name, _number) as identifer, count(_id) as amount from call group by identifer
And I wanna use it in ContentConsolver but it can't work with "group by":
String[] projections = new String[] { "ifnull(name, number) as identifer", "count(_id) as amount" };
String group = "identifer";
//String selection = ") GROUP BY (" + group;
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, projections, null, null, null /*!group*/ );
What should I do?
#njzk2 did the trick by using HashSet: Group By in ContentResolver in Ice Cream Sandwich, But it didn't work with count() if you want sum.
I think the best solution is to make a copy of CallLog Database, then you can use rawQuery() or whatever you want (maybe it is a waste of performance).
private void refreshDbCache()
{
CallDbCache dbCache = new CallDbCache(this);
dbCache.clear(CallDbCache.TB_CALL);
String[] columns = new String[] { CallLog.Calls._ID, CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME };
Cursor cursor = getContentResolver().query(URI_CALL, columns, null, null, null);
while (cursor != null && cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
dbCache.insert(CallDbCache.TB_CALL, new LogData(id, number, name, "", "", "", ""));
}
cursor.close();
dbCache.close();
}
i am trying to do a query of my database for a string lets call it "Test" and then find out what row that particular string is in and save that number to use. I thought i had this figured out before but now it is not working for some reason and i get an error saying no such column "Test".
here is my code
public String getRow(String value){
ContactDB db = new ContactDB(this);
db.open();
Cursor curs = db.getId(value);
String test = curs.getString(curs.getColumnIndex(db.NAME));
curs.close();
Log.v("Contact", "Row ID: " + test);
db.close();
return test;
}
"Test" is sent into that as value
this is in my database
//---retrieve contact id---
public Cursor getId(String where){
Cursor c = db.query(DATABASE_TABLE, new String[] {ID},where,null,null,null,null);
if (c != null)
c.moveToFirst();
return c;
}
i dont remember changing anything from when i first tested it so i dont know why it wont work now
There are 2 errors that i could notice:
In the query
Cursor c = db.query(DATABASE_TABLE, new String[] {ID},where,null,null,null,null);
only the ID column is selected whereas you are trying to fetch details for column NAME
String test = curs.getString(curs.getColumnIndex(db.NAME));
include the name column as well in the select clause : something like
Cursor c = db.query(DATABASE_TABLE, new String[] {ID,NAME},where,null,null,null,null);
In the where clause you need to write the condition string excluding "where"
in your case String where contains value "Test". Hence the filter condition should be as
String whereClasue = NAME + " = '" + where + "'";
The query should be something like this:
public Cursor getId(String where){
Cursor c = db.query(DATABASE_TABLE, new String[] {ID,PHONE_NUMBER,NAME},NAME + " = '" + where + "'",null,null,null,null);
if (c != null)
c.moveToFirst();
return c;
}