Android Sqlite get last insert row id

Possible Duplicate:
Get generated id after insert

I want to get the last inserted row id in my android application using this code :

  • How to realize this custom popup menu with Material Design Android?
  • Stuck on emulator screen in Eclipse with Android Development?
  • Coordinator layout custom layout behavior never getting called
  • Start Android Service after every 5 minutes
  • Apache HttpClient Android (Gradle)
  • Android. How to use the same localizable messages for all the languages in a country?
  • String query = "SELECT * from SQLITE_SEQUENCE";
    int createdUserId = Integer.parseInt(dbHelper.executeSQLQuery(query).toString());

    but the problem is that it’s throws an exception that cannot convert dbHelper.executeSQLQuery(query).toString() to integer. I’m not really good at sqlite ,but i think that this should return the last row id which was inserted…which will definitely will be int (at least I think this way). So if this is not the right way, can someone please guide me how to get the last row id in android application.


    Related posts:

    Tips for optimizing a website for Android's browser?
    adb shell dumpsys iphonesubinfo not working since Android 5.0 Lollipop
    Only the original thread that created a view hierarchy can touch its views
    How to use setDuration() method in SnackBar (Android Design Support Library)
    Facebook key hash does not match any stored key hashes
    Best practice android:onClick XML attribute or setOnClickListener?
  • What's the difference between “minifyEnabled” and “useProguard” in the Android Plugin for Gradle?
  • Remove all debug logging calls before publishing: are there tools to do this?
  • Custom toast in android : a simple example
  • Why does GoogleMap.animateCamera() crash after onGlobalLayout()?
  • Update fragment on dialog fragment option selection
  • drag and drop listitem of a listview to another listview's listitem
  • 2 Solutions collect form web for “Android Sqlite get last insert row id”

    Your SQL statrment will return all the row ids, not just the latest. Try something like this…


    Also note that I believe selecting from SQL_LITE_SEQUENCE will get the latest ID from ANY table, you can also access the SQL_LITE_SEQUENCE by selecting ROWID on any table, and getting just the IDs for that table. IE

    SELECT ROWID from MYTABLE order by ROWID DESC limit 1

    And thanks to MisterSquonk for pointing out the next step in the comments, adding it here for ease of reference later…

    The query statement will then return a Cursor object containing the results, so to access the integer value you would do something like this (I’ll substitute more common methods for your helper method, just for others sake)

    String query = "SELECT ROWID from MYTABLE order by ROWID DESC limit 1";
    Cursor c = db.rawQuery(query);
    if (c != null && c.moveToFirst()) {
        lastId = c.getLong(0); //The 0 is the column index, we only have 1 column, so the index is 0

    (Note that although the SQL Lite docs call ROWID and Integer, it is a 64 bit integer, so in Java it should be retrieved as a long.)

    Care to use “selectlast_insert_rowid()“? 🙂

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.