Reputation: 185
I saw some solutions for this in MySQL, but can't seem to find anything for SQLite. I am trying to get values for the 7 most recent entries in my database. I know how to order them by date, but for some reason I can't find the syntax for returning a given number of them.
This is the query, using JDBC
PreparedStatement get_recent_id = conn.prepareStatement("SELECT * FROM workout_log ORDER BY t desc");
This will return them ordered, but I was thinking SELECT 7 *
....would work, but this is the error I get
Exception in thread "main" java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near "FROM": syntax error)
at org.sqlite.DB.newSQLException(DB.java:383)
at org.sqlite.DB.newSQLException(DB.java:387)
at org.sqlite.DB.throwex(DB.java:374)
at org.sqlite.NativeDB.prepare(Native Method)
at org.sqlite.DB.prepare(DB.java:123)
at org.sqlite.PrepStmt.<init>(PrepStmt.java:42)
at org.sqlite.Conn.prepareStatement(Conn.java:404)
at org.sqlite.Conn.prepareStatement(Conn.java:399)
at org.sqlite.Conn.prepareStatement(Conn.java:383)
at WeeklyReport.main(WeeklyReport.java:38)
Upvotes: 0
Views: 202
Reputation: 6525
Add only limit 0,7
The query is :-
SELECT * FROM workout_log ORDER BY t desc limit 0,7;
Upvotes: 0
Reputation: 40990
I think this will be helpful. Use limit
SELECT * FROM workout_log ORDER BY t desc limit 7
Upvotes: 1
Reputation: 16699
SELECT * FROM workout_log ORDER BY t desc Limit 7;
Also note that using *
can lead to some problems down stream. What if your columns change order or you add new one? Think of listing your fields.
Upvotes: 2