How to update datas with ContentProvider? - android

How can I execute this sql with Content Provider as below:
update dtrips set dtp_day_idx=dtp_day_idx+2 where tp_id=1
My java code is like this
DTrip dTrip = new DTrip();
ContentValues values = createContentValues(dTrip);
values.put("dtp_day_idx" ,...);
String select ="tp_id="+tripId;
mContentResolver.update(DTripColumns.CONTENT_URI, values, select, null);
Can anyone help me fix the code?
Thanks.

Append the ID for the row to the content URI like this: (assuming DTripColumns.CONTENT_URI is the content URI of your provider)
final Uri uri = ContentUris.withAppendedId(DTripColumns.CONTENT_URI, tripId);
mContentResolver.update(uri, values, null, null);
Here is more information: http://developer.android.com/guide/topics/providers/content-provider-basics.html

Related

Confusion in ContentPorvider's CRUD operations

I am new to contenProvider and am reading a tutorial on how to do CRUD operations on UserDictionary.
For Query:
resolver.query(UserDictionary.Words.CONTENT_URI, projection, null, null, null);
For insert:
resolver.insert(UserDictionary.Words.CONTENT_URI, values); //ContentValues values = new ContentValues();
For update:
Uri uri = ContentUris.withAppendedId(Words.CONTENT_URI, id);
long noUpdated = resolver.update(uri, values, null, null);
for delete:
long noDeleted = resolver.delete(Words.CONTENT_URI,
Words.WORD + " = ? ", new String[]{"Zaphod"});
My confusion is in update and delete operations.
In update: why is it using Words.CONTENT_URI in withAppendedId() ? shouldn't it be using UserDictionary.Words.CONTENT_URI ?
Also in delete: its not using withAppendedId(). Still why is it using Words.CONTENT_URI ?
In your example UserDictionary.Words.CONTENT_URI is what identifies the data in a provider, so the data location, the appended Id is what identifies a unique record.
So, by your examples:
1) For Query, is using UserDictionary.Words.CONTENT_URI to return all the records under this content Uri address. If you wanted to return only one specific record, from which you already know its Id, then it could also be as follows:
Uri uri = ContentUris.withAppendedId(Words.CONTENT_URI, id);
resolver.query(uri, projection, null, null, null);
3) For Insert, no Id is used because the record doesn't exist yet. What the operation does is to insert a new record into the database, and the return value will be the new record Uri, which will include the new Id.
3) For Update, the appended Id is used to identify which specific record must be updated. Updating UserDictionary.Words.CONTENT_URI without a record Id would result in the update of all the records.
4) For Delete, your example is deleting all the records where the column Words.WORD has the value "Zaphod", which may result in 0, 1, or multiple records being deleted.
If you wanted to delete only 1 specific record from which you know its Id, then it would be as next:
Uri uri = ContentUris.withAppendedId(Words.CONTENT_URI, id);
resolver.delete(uri, null, null);
Answering your last question, there is no difference between Words.CONTENT_URI and UserDictionary.Words.CONTENT_URI. The UserDictionary section is a qualifier, which can be removed if has been added as an import on top of the java file, this allows not having to type it all the time, but at the end both are actually the same thing and makes no difference.

getContentResolver().delete filter not effect

i want to delete "TYPE=0" record,my code:
cv.put(Call.NAME, titleString);
cv.put(Call.BUILD_NAME, "");
cv.put(Call.BUILD, build);
cv.put(Call.UNIT, unit);
cv.put(Call.ROOM, room);
cv.put(Call.TIME, date);
cv.put(Call.TYPE, 0);//
cv.put(Call.DEVICETYPE, doordevicetype);
cv.put(Call.PHOTO, baos.toByteArray());
cv.put(Call.CONTACT_ID, 9999);
Uri contactUri, uriRet;
ContentResolver resolver = getContentResolver();
uriRet = resolver.insert(contactUri, cv);
so I can insert many records,some record Call.TYPE=0 ,some Call.TYPE=1, so I want to delete all Call.TYPE=1. My code:
int a=getContentResolver().delete(Call.CONTENT_URI, Call.TYPE +"=" +1, null);
but the app delete all records,include Call.TYPE=0.I do not know what is wrong. Add,insert,update record all is ok,but delete some reocrds not effect, can you give some advice, please?
use this code to delete particular row when match TYPE = 1...
String[] selectionArgs=new String[]{String.valueOf(1)};
// this is for which argument to match with TYPE=1 and delete row
String selection=""+TablenName.TYPE+"=?"; // this is where condition
getContentResolver().delete( Call.CONTENT_URI, selection, selectionArgs);
Note : just refer this [link][1]. its usefull to understand for , how delete are happening..
[1]: http://www.w3schools.com/sql/sql_delete.asp

simple code, but cursor is still returning null from content provider

Got a problem here with a simple section of code that uses managedQuery cursors. Two parts, the top half of the code puts a string into the LATITUDE column of the MediaStore database content provider.
The second part of the code below that reads that same string back from the database. This is where it is returning a null result. Either because the string was not correctly read into the database in the first part of the code or there is an error in the second part where it reads it back from the database.
I am using the LATITUDE column of the Media.images content provider to store a string. There is no other unused column that is available so that is why I am using it.
The goal is to put the string path name of the mp3 file into the LATITUDE column of an image and read it back out again later with another query.
I tracked down the problem to the following code. The cursor in the second part of code is returning null. Is there something wrong with my use of cursors, or some error in this that I don't know about?
String displayName; // string pathname of the mp3 file to be put into LATITUDE column
String filename; // the pathname of the image that I want to add the database info to
ContentValues imageValues = new ContentValues();
String selection3 = MediaStore.Images.Media.DATA + "='" + filename +"'";
imageValues.put(MediaStore.Images.Media.LATITUDE, displayName);
getContentResolver().update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
imageValues, selection3, null);
String[] proj6 = { MediaStore.Images.Media.LATITUDE };
String selection6 = MediaStore.Images.Media.DATA + "='" + filename +"'";
Cursor cursor2 = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
proj6, selection6, null, null);
cursor2.moveToFirst();
String displayer = (String)
cursor2.getString(cursor2.getColumnIndex(MediaStore.Images.Media.LATITUDE));
Found out that there was not an error like I thought it would be. It was only a typing error.
put method has "Images.Media.LATITUDE"
update method has "Audio.Media.LATITUDE", changed this to "Images.Media.LATITUDE" like it is in the put method and it works now.
imageValues.put(MediaStore.Images.Media.LATITUDE, displayName);
getContentResolver().update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
imageValues, selection3, null);

Update display_name of contact Android

This method always return false. I would like update display name of contact...
public int updateDisplayName(long id, String newname) {
Uri contact = ContentUris.withAppendedId(Contacts.CONTENT_URI, id);
ContentValues values = new ContentValues();
values.put(Contacts.DISPLAY_NAME, newname);
return context.getContentResolver().update(contact, values, null, null);
}
Can you help me?
Thanks,
Mateus
That's not surprising, this table is for aggregating information of multiple accounts and most of its data is only manipulated by the Contacts content provider itself.
See the documentation: http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html
You would have to change the raw contact.

How to use own ContentProvider to other application?

I have created a my own ContentProvider by extending class ContentProvider with all its abstract methods.
I am able to use MyContentProivder in my application where I created it,but I
am not getting how can I use it in other application.
This question might have been asked many times but I am really not getting any information.
Please Help
If you have your Custom ContentProvider you can use it in other application with its URI.
To insert,
ContentValues values = new ContentValues();
values.put("title", "lalit");
values.put("isbn", "0470285818");
Uri uri = getContentResolver().insert(Uri.parse(URI), values);
To read,
Uri allTitles = Uri.parse(URI);
Cursor c = managedQuery(allTitles, null, null, null, null);
In the same way you can delete, update using query.

Categories

Resources