I use sqlite.

Rows are continuously added to the database (using code). I need to get the last line written there.

How to do it?


Record date is.

  • one
    In the current form, it is impossible to give a concise answer to your question. To get an answer, explain exactly what you see the problem, what technologies you use and what you want to see in the answer. - Kromster February
  • @KromStern, Updated the question. - DenShDen
  • one
    If you have for example a record id that is constantly increasing or for example the date of adding a record, then just take the first record with reverse sorting by id (date) - Mike
  • Record date is. And please tell me the code for reverse sorting. - DenShDen
  • 2
    @DenShDen SELECT smth FROM table ORDER BY column DESC LIMIT 1 - Alexey Shimansky

2 answers 2

Reply from comments from Alexey Shimansky

 SELECT * FROM tbl ORDER BY created_at DESC LIMIT 1 

    Execute SELECT last_insert_rowid() query SELECT last_insert_rowid() to DB. The result will be a record containing the last ID added.

    If autoincrement primary key then you can use this query select seq from sqlite_sequence where name="table_name" . It returns the current value of the increment, which will be the key to the last added record.

    • five
      last_insert_rowid() returns the last id added in this session . In another session that did not insert, this function would not do anything. - Mike