1

I want to get the last_insert_rowid() after my insert statement.

I wanted to use SQLiteDatabase.execSQL() but it says that I can't use select statements there.

How can I use last_insert_rowid()?

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • 1
    Please check the below thread http://stackoverflow.com/questions/4017903/get-last-inserted-value-from-sqlite-database-android It may help you. – Amit Apr 05 '13 at 11:53
  • Also, You can use db.rawQuery("SELECT last_insert_rowid()", null), and get the value from the cursor. – Amit Apr 05 '13 at 11:57
  • @Amit tnx this is what I been looking for – Ilya Gazman Apr 05 '13 at 12:03

1 Answers1

2

I think you have used this method for insert data in sqlite database its give you new inserted row.

No need to use any another method.

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

Parameters

table the table to insert the row into

nullColumnHack optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.

values this map contains the initial column values for the row. The keys should be the column names and the values the column values

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

like:

long a = database.insert("animals", null, values);

a is your table's row id.

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177