Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

sql - How to use the LIMIT argument in an SQLite Query with Android

I am trying to use the following query to get the most recent result by date.

Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_DATE, KEY_REPS, 
KEY_WEIGHT}, null, null, null, null, KEY_DATE + "DESC", ???);

I need to use the limit argument (I believe) but it takes a string. I tried creating a string with a value of "1" but that didn't work. Other things I tried "1" LIMIT 1 "LIMIT 1" Limit 1 "Limit 1"

Also, if anyone knows of a great reference site (other than this one) that actually shows you various SQL queries (for ANDROID) that would be very helpful...

EDIT The error I got from using "1"...perhaps the limit isn't my problem? Here is the error: android.database.sqlite.SQLiteException: no such column: dateDESC: , while compiling: SELECT date, repetitions, weight, FROM TEST ORDER BY dateDESC LIMIT 1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Order by id DESC Limit 1:

db.query("table", null, "column=?", new String[]{"value"}, null, null, "id DESC", "1");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...