How to get Last record from Sqlite?

前端 未结 12 1995
悲&欢浪女
悲&欢浪女 2020-12-02 07:37

I have a one table question_table and one ImageButton (Back). I need to get the last inserted record from the database after clicking on t

相关标签:
12条回答
  • 2020-12-02 08:31

    Another option is to use SQLites LAST_VALUE() function in the following way.

    Given this table:

    +--------+---------+-------+
    | OBJECT |  STATUS |  TIME |
    +--------+---------+-------+
    |        |         |       |
    | 1      |  ON     |  100  |
    |        |         |       |
    | 1      |  OFF    |  102  |
    |        |         |       |
    | 1      |  ON     |  103  |
    |        |         |       |
    | 2      |  ON     |  101  |
    |        |         |       |
    | 2      |  OFF    |  102  |
    |        |         |       |
    | 2      |  ON     |  103  |
    |        |         |       |
    | 3      |  OFF    |  102  |
    |        |         |       |
    | 3      |  ON     |  103  |
    +--------+---------+-------+
    

    You can get the last status of every object with the following query

    SELECT                           
        DISTINCT OBJECT,             -- Only unique rows
        LAST_VALUE(STATUS) OVER (    -- The last value of the status column
            PARTITION BY OBJECT      -- Taking into account rows with the same value in the object column
            ORDER by time asc        -- "Last" when sorting the rows of every object by the time column in ascending order
            RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING    -- Take all rows in the patition
        ) as lastStatus
    FROM
        TABLE
    

    The result would look like:

    +--------+--------------+
    | OBJECT |  LAST_STATUS |
    +--------+--------------+
    |        |              |
    | 1      |  ON          |
    |        |              |
    | 2      |  ON          |
    |        |              |
    | 3      |  ON          |
    +--------+--------------+
    
    0 讨论(0)
  • 2020-12-02 08:32

    in sqlite, there is a table called sqlite_sequence, this table contains the table name and it's last id number (if the id is auto incremented).

    So, to get the last row in a table just put :

    Select * from TABLENAME where id=(SELECT * from sqlite_sequence where name ='TABLENAME')
    
    0 讨论(0)
  • 2020-12-02 08:35

    Just simple, you can move with Cursor moveToLast(); method provides to move to the last record

    cursor.moveToLast();
    
    0 讨论(0)
  • 2020-12-02 08:36

    The previous answers assume that there is an incrementing integer ID column, so MAX(ID) gives the last row. But sometimes the keys are of text type, not ordered in a predictable way. So in order to take the last 1 or N rows (#Nrows#) we can follow a different approach:

    Select * From [#TableName#]  LIMIT #Nrows# offset cast((SELECT count(*)  FROM [#TableName#]) AS INT)- #Nrows#
    
    0 讨论(0)
  • 2020-12-02 08:37

    I wanted to maintain my table while pulling in one row that gives me the last value in a particular column in the table. I essentially was looking to replace the LAST() function in excel and this worked.

    , (Select column_name FROM report WHERE rowid = (select last_insert_rowid() from report))
    
    0 讨论(0)
  • 2020-12-02 08:40

    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

    0 讨论(0)
提交回复
热议问题