Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Last record from Sqlite?

Tags:

android

sqlite

People also ask

How can I select the last record of a table?

To get the last record, the following is the query. mysql> select *from getLastRecord ORDER BY id DESC LIMIT 1; The following is the output. The above output shows that we have fetched the last record, with Id 4 and Name Carol.

How do I find the last row ID in SQLite?

SQLite has a special SQL function – last_insert_rowid() – that returns the ID of the last row inserted into the database so getting the ID of a new row after performing a SQL insert just involves executing the last_insert_rowid() command.

Does SQLite store history?

Like Safari, they also use SQLite to store user data and browser history.

How do I find data in SQLite?

To Search for the values define edit text and implement text watcher in database enter a query as shown below: editText. addTextChangedListener(new TextWatcher(){ Cursor cusror; cursor=db. rawQuery("SELECT * FROM "+ DB_NAME + " WHERE " + DB_NAME.id + " = " + DB_NAME.Id + " AND " + DB_NAME.


I think the top answer is a bit verbose, just use this

SELECT * FROM table ORDER BY column DESC LIMIT 1;

Try this:

SELECT * 
    FROM    TABLE
    WHERE   ID = (SELECT MAX(ID)  FROM TABLE);

OR

you can also used following solution:

SELECT * FROM tablename ORDER BY column DESC LIMIT 1;

To get last record from your table..

 String selectQuery = "SELECT  * FROM " + "sqlite_sequence";
 Cursor cursor = db.rawQuery(selectQuery, null);
  cursor.moveToLast();

Here's a simple example that simply returns the last line without need to sort anything from any column:

"SELECT * FROM TableName ORDER BY rowid DESC LIMIT 1;"       

I think it would be better if you use the method query from SQLiteDatabase class instead of the whole SQL string, which would be:

 Cursor cursor = sqLiteDatabase.query(TABLE, allColluns, null, null, null, null, ID +" DESC", "1");

The last two parameters are ORDER BY and LIMIT.

You can see more at: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html