4

I perform an insert on a database and I want to know what the rowid is for that insert.

Does this accomplish the task:

Uri uri = ContentResolver.insert(url,values);    //Make insert
int rowid= Integer.parseInt(uri.getFragment());  //Get rowid

The Android documentation states that the insert returns "the URL of the newly created row." And the Uri method getFragment() is supposed to return everything after the /#. So, is my understanding correct that getFragment() would thus return to me the rowid?

Squonk
  • 48,735
  • 19
  • 103
  • 135
user817129
  • 522
  • 3
  • 10
  • 19

1 Answers1

6

To parse the URI after an insert and get the id you should use ContentUris.parseId(uri).

mgv
  • 8,384
  • 3
  • 43
  • 47
  • This provides me the rowid of the last inserted row? – user817129 Jun 28 '11 at 00:51
  • How is this function any different from my code? The insert statement returns a Uri of the row and from that I call getFragment to extract out the id. In the Uri, after the "/#" would be all numeric - the rowid, no? – user817129 Jun 28 '11 at 01:06