Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ID from insert()

Tags:

android

sqlite

I've got a little problem with retrieving ID from added row. insertWithConflict inside my ContentProvider returns the ID of freshly added row but I don't know a way to get it from my ContentProvider to my IntentService responsible for firing insert.

CODE

Insert in IntentService

Uri uri = Uri.parse(MicroDatabaseProvider.CONTENT_URI + "/" +  MicroDatabaseProvider.ORGANIZER_SEARCH_IN);
getContentResolver().insert(uri, cv);

Insert in ContetnProvider

long idLong = sqlDB.insertWithOnConflict(DbHelperMicro.ORGANIZER_SEARCH_TABLE,
                    null,
                    values,
                    SQLiteDatabase.CONFLICT_IGNORE);
            getContext().getContentResolver().notifyChange(Uri.parse(CONTENT_URI + "/" + ORGANIZER_SEARCH_QUE), null);
like image 553
JakubW Avatar asked Dec 19 '22 11:12

JakubW


1 Answers

Your insert function in your ContentProvidershould return an Uri value like the following:

public Uri insert(Uri uri, ContentValues values) {
//...
return Uri.parse(base_uri+ "/" + id); //where base uri is the base uri from your table
}

If it is like I have shown you, you can retreive the id from that URI.

Use the following code in your IntentService

Uri result = getContentResolver().insert(uri, cv); 

long id = Long.parseLong(uri.getLastPathSegment());
like image 55
zozelfelfo Avatar answered Jan 09 '23 09:01

zozelfelfo