How to get Last record from Sqlite?
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;
How to read the last record in SQLite table?
There is a function named sqlite3_last_insert_rowid() which will return the integer key for the most recent insert operation. http://www.sqlite.org/c3ref/last_insert_rowid.html
This only helps if you know the last insert happened on the table you care about.
If you need the last row on a table, regardless of wehter the last insert was on this table or not, you will have to use a SQL query
SELECT * FROM mytable WHERE ROWID IN ( SELECT max( ROWID ) FROM mytable );
SQLite: How to get last record?
using this code:
select * from my_table order by rowid desc LIMIT 1
rowid
is built-in field in sqlite
tables
Read More : https://www.sqlite.org/lang_createtable.html#rowid
How to get last records with conditions from SQLite?
SQLite has a rowid
that you can query over it like this:
select *
from Table1 t
where exists (
select 1
from Table1 ti
where t.course = ti.course
group by ti.course
having t.rowid = max(ti.rowid); -- filtering rows that are newest rows in table
);
SQLite Fiddle Demo
Sample Data:
| id | course | ... hidden rowid |
|----+-----------| ... -------------|
| 1 | math | ... 1 |
| 2 | english | ... 2 |
| 3 | math | ... 3 |
| 4 | english | ... 4 |
| 5 | chemistry | ... 5 |
| 8 | test | ... 6 |
| 7 | test | ... 7 |
Result:
| id | course | ... hidden rowid |
|----+-----------| ... -------------|
| 3 | math | ... 3 |
| 4 | english | ... 4 |
| 5 | chemistry | ... 5 |
| 7 | test | ... 7 |
Retrieve Last Record in SQLite Table (again)
The two queries you show are equivalent.
As to whether they return the last record, that depends on what you mean by "last." If you delete rows you cannot rely on either query to return the last inserted row.
Fetching last row from sqlite database
If you have already got the cursor
, then this is how you may get the last record from cursor
:
cursor.moveToPosition(cursor.getCount() - 1);
then use cursor
to read values
or
do it like this
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToLast();
or
SELECT * FROM TABLE WHERE ID = (SELECT MAX(ID) FROM TABLE);
Android / Java: How to get last row value in sqlite?
you are closing cursor before getting it's value
try this :
String selectQuery= "SELECT * FROM " + TABLE_XYZ+" ORDER BY column DESC LIMIT 1";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
String str = "";
if(cursor.moveToFirst())
str = cursor.getString( cursor.getColumnIndex(KEY_TEXT) );
cursor.close();
return str;
get first and last element from sqlite database
SQLite does support UNION
but does not like the queries inside the parentheses.
Use each of your queries as a subquery:
select * from (select * from location order by id ASC LIMIT 1)
UNION
select * from (select * from location order by id DESC LIMIT 1);
Related Topics
Java.Io.Ioexception: Permission Denied Cannot Create File
Suppress Keyboard from Popping Up
How to Set Android_Ndk_Home So That Android Studio Does Not Ask for Ndk Location
Remove Items from Listview With a Custom Adapter
Import Android.Support.V7.App Cannot Be Resolved
How to Know Which Fragment the User Is Viewing
Flutter Keyboard Makes Textfield Hide
How to Turn a Portable Sd Card into Internal Storage Via Adb Command
R Cannot Be Resolved - Android Error
React Native: Java_Home Is Not Set and No 'Java' Command Could Be Found in Your Path
Open Play Store App from Browser Link
How to Disable Spinner in Android and Enable It on Button Click
Download Manager Progress Not Visible in the Notification Area
How to Know Location Area Code and Cell Id in Android Phone
Install_Failed_User_Restricted:Android Studio Using Redmi 4 Device