Note that there is no “last row” in an SQL database. If you don’t specify a WHERE or ORDER BY then the order in whichb the rows are returned is undefined.
Otherwise, here your statement compiled as soon as I added db as SQLiteDatabase to app.
If you simply want the record of the database that has largest ID, you can more efficiently use the following SQL:
Var rs as RowSet = db.SelectSQL("select * from person order by id desc limit 1")
It’s one query, rather than two. The subquery that retrieves the max id and the other that selects that row. So long as there’s an index on ID it should be very quick.
Emile, we are making assumptions about your fields - probably correct assumptions, but assumptions none the less.
For example, if “id” is just a field of data then you may not get the last record unless the record with max(id) is sorted to the bottom ahead of time or via the “order by” that Ian gave. Also, although “id” may be thought of as being unique, That doesn’t mean there is only one record with the max(id) - for example, a database that hold records of customer purchases (multiple records for the same customer).
When possible, it’s handy to provide a small sample of what the data looks like. Next, given that sample, what would you’d like the result of the code to look like? That way we can recommend how to get from here (your starting data) to there (the result you seek). Maybe it requires fixing a problem with your provided code statements; maybe it’s recommending a more efficient way of writing the SQL.
When I have a problem with a database, I find it helpful to leave the explicit situation alone and create a small example that will exercise just the action I’m looking for. For example, for your case, it seems like I’d make a database of five different fruits (apple, orange, cherry, pear, lemon). With your data example, I’d see how your “id” is determined/created. Then I’d run an SQL statement to create the result you want. If it works, then one can compare the code that works with what doesn’t and see what needs adjustment or replacement in the original code.