Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6618661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:51:49+00:00 2026-05-25T20:51:49+00:00

Possible Duplicate: Get generated id after insert I want to get the last inserted

  • 0

Possible Duplicate:
Get generated id after insert

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

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.

Thanks!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:51:50+00:00Added an answer on May 25, 2026 at 8:51 pm

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

    SELECT ROWID from SQL_LITE_SEQUENCE order by ROWID DESC limit 1
    

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to get the insert ID in JDBC? Hi, I'm using JDBC
Possible Duplicate: How do I get the HMODULE for the currently executing code? I'm
Possible Duplicate: PHP detecting request type (GET, POST, PUT or DELETE) This should be
Possible Duplicate: PHP ToString() equivalent I get this error: Catchable fatal error: Object of
Possible Duplicate: Return value from thread I want to get the free memory of
Possible Duplicate: Best Way to Sprite Images? Hi i have been using this http://spritegen.website-performance.org
Possible Duplicate: Get connecting IP from specified ports that using by other program. If
Possible Duplicate: random string generation - two generated one after another give same results
Possible Duplicate: Get the length of an audio file php i am using php
Possible Duplicate: Get script of SQL Server data I want to move some rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.