I want take data from a row with two parameters, and only return the first.
This is my code:
String[] projection = { VehiculoContentProv.Consumos.ID_VEH_C,
VehiculoContentProv.Consumos.ID_CALCULO_KL,
VehiculoContentProv.Consumos.ID_CALCULO_MG};
String selection = "_idv =" + idV1 + " AND " + idV2;
cursorLoader = new CursorLoader(this, VehiculoContentProv.CONTENT_URI_CONSUMO,
projection,
selection, null, null);
thanks a lot
Related
I have this code below in which gets all data having the 'grpnumber'. my question is: How can I get 2 where clause? i want to get the records having the group number and calories?
String[] projection = {
FoodReaderContract.FoodGroupEntry._ID,
FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_NUMBER,
FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_FOODNAME,
FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_TIMEOFDAY,
FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_CALORIE
};
// Filter results WHERE "title" = 'My Title'
String selection = FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_NUMBER
+ " = ?";
String[] selectionArgs = {grpnumber};
//String selection = FeedReaderContract.FooEntry._ID + " = ?";_
//Int[] selectionArgs = {2};
// How you want the results sorted in the resulting Cursor
String sortOrder =
FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_NUMBER + " DESC";
Cursor cursor = db.query(
FoodReaderContract.FoodGroupEntry.GROUP_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
sortOrder // The sort order
);
You can use the AND keyword.
String selection = FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_NUMBER + " = ? "
+ " AND " + FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_CALORIE + " = ?";
String[] selectionArgs = {String.valueOf(grpnumber), String.valueOf(calorie)};
I basically can not accomplish this for over 2 weeks now.
I have posted a numerous amount of code over a few questions yet most of them got ignored so I won't flood this question with more code of my own that won't even be read.
How do I search the MediaStore with "LIKE" attributes using a String?
E.G. I type in Shoot To Thrill, I receive song ID with this code:
if(cursor.moveToFirst()){
while(cursor.moveToNext()){
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
test.setText(title +" " + artist + " " + id);
}
}
Here is something to begin with:
String[] projection = {
BaseColumns._ID,
MediaStore.Audio.Artists.ARTIST,
MediaStore.Audio.Media.TITLE
}
Cursor cursor = this.managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, //uri
projection, //projection
//i dont know what to enter,
//i dont know what to enter,
MediaStore.Audio.Media.TITLE);
It's just normal SQL hiding behind the scenes. Normal LIKE operations should work just fine. You can use MediaStore.Audio.Media.TITLE + " LIKE \"%thrill%\"" just like any other SQL query.
String[] projection = { BaseColumns._ID,
MediaStore.Audio.Artists.ARTIST, MediaStore.Audio.Media.TITLE };
String where = MediaStore.Audio.Media.TITLE + " LIKE ?";
String[] params = new String[] { "%life%" };
Cursor q = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection, where, params, MediaStore.Audio.Media.TITLE);
try {
while (q.moveToNext()) {
Log.e("song", q.getString(1) + " " + q.getString(2));
}
} finally {
q.close();
}
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);
I was just curious about what the MediaStore.Images.Media.MINI_THUMB_MAGIC column contains. Does it have anything to do with the image's thumbnail? Thank you.
See MediaStore.Images.ImageColumns from the Android developers ref. for the doc -- which says it returns "the mini thumb id".
Also see Displaying Thumbnail Photos on Map for how to use this ID:
long thumbId = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns.MINI_THUMB_MAGIC));
String[] args = new String[]{String.valueOf(thumbId)};
Cursor ct = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Thumbnails._ID + "= ?", args, null);
MINI_THUMB_MAGIC returns a random number and you can not get thumbnail id from this. Use MediaStore.Images.Media._ID, it is the same number as MediaStore.Images.Thumbnails.IMAGE_ID
ArrayList<String> ids; // ArrayList of MediaStore.Images.Media._ID
String selection = MediaStore.Images.Thumbnails.IMAGE_ID + " IN (" + TextUtils.join(",", ids) + ")";
Cursor cursor2 = resolver.query(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
new String[]{{MediaStore.Images.Thumbnails.DATA,MediaStore.Images.Thumbnails.IMAGE_ID}},
selection,
null,
null);
I have a SQlite database with some columns and with a content of an EditText I would like to select some rows from the database.
In main activity:
case R.id.betx:
String dates2 = sqletx.getText().toString();
GlobalVars.settables(dates2);
Intent intentstar2t = new Intent(dbhelp.this, CustomList.class);
startActivity(intentstar2t);
break;
This is my main activity, the 'dates2' String is the content of the edittext, and I save it for the setTables method.
My code in the hornot class where:
public Cursor getTable() {
String deda = GlobalVars.gettables();
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS,
KEY_CALORIE, KEY_MULTI, KEY_DATE};
return ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "=" + deda ,
null, null, null, null);
}
Here I create a Cursor for the rows that fulfill the requirements in the last row. The String deda will be the 'dates2' we set before and I would like to get the rows where the KEY_DATE is equivalent to dates2.
The listview activity where I list the suitable rows.
if (todoItems.size() > 0) {
todoItems.clear();
}
if (list.size() > 0) {
list.clear();
}
Cursor c = info.getTable();
c = info.getTable();
if (c.moveToFirst())
{
do{
todoItems.add(c.getString(0) + " " + c.getString(1) + " " + c.getString(2)+ " " + c.getString(3)+ " " + c.getString(4));
quanitems.add(c.getString(4));
}
while (c.moveToNext());
}
if (todoItems.size() > 0)
{
for (int i=0; i<todoItems.size(); i++)
{
each=new EachRow();
each.text=(String) todoItems.get(i);
list.add(each);
each2=new EachRow();
each2.text=(String) quanitems.get(i);
list2.add(each2);
}
listView.setAdapter(new MyAdapter(this, 0, list));
}
I use a custom listview, in every row there are some items, but the point is that I create a cursor (c) for the rows where the KEY_DATE is equivalent to dates2.
My problem is I never define integer variable here, but if I create an entry where the date is for example "107" or "6519684" it is working, but if it is "tableOne" or any text it crushes at the row:
return ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "=" + deda , null, null, null, null);
and
Cursor c = info.getTable();
(as you can see both problems are connected to the getTable method.
I use only Strings so practically the numbers ("107") are strings as well.
Any idea?
Thanks in advance!
use:
return ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "=?" ,
new String[] { deda }, null, null, null);
Edit(explaination):
in query method you have String selection and String[] selectionArgs parameters, so in selection you can use ? for parameters in selectionArgs. Now underlying query builder will build query with this paramater and will apply proper escape chars.
I think that your query should be like this:
return ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "= ?" ,
new String[] {deda}, null, null, null);