-1

I have a sqlite table named as 'TX_TABLE' and the primary key Id name is TX_UID. I insert a row into this table and would like to get the value of this unique id column 'TX_UID". How to get it?

db.insert(VivzHelper.TX_TABLE, null, contentvalues);
String td = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
helper.close();
CL.
  • 173,858
  • 17
  • 217
  • 259
Rajendran
  • 1
  • 2
  • 2

2 Answers2

1

Assuming that you are using the Android SQLiteDatabase class, the insert() call returns the row id directly:

public long insert (String table, String nullColumnHack, ContentValues values)

Returns the row ID of the newly inserted row, or -1 if an error occurred

Manuel Barbe
  • 2,104
  • 16
  • 21
0

You can use the function last_insert_rowid() (click on function name for details) to get the last inserted id.

From documentation:

The last_insert_rowid() function returns the ROWID of the last row insert from the database connection which invoked the function

It is possible to retrieve it using the following sql command

SELECT last_insert_rowid()
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56