Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

sql

limit

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

like image 355
easycheese Avatar asked Jun 15 '11 17:06

easycheese


People also ask

What is use of limit clause with select query in android?

The LIMIT clause is an optional part of the SELECT statement. You use the LIMIT clause to constrain the number of rows returned by the query. For example, a SELECT statement may return one million rows.

Does SQLite support limit?

SQLite can have a maximum database size of 140 terabytes (TB). A SQLite database is a set of one more pages where every page is the same size. Maximum size of a page cannot exceed 65536 bytes. The maximum size of a database file is 2147483646 pages.

Which method is used in Android to executing SQLite queries?

We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data. This method return the total number of columns of the table.

Can I use SQLite in Android?

Android SQLite is a very lightweight database which comes with Android OS. Android SQLite combines a clean SQL interface with a very small memory footprint and decent speed. For Android, SQLite is “baked into” the Android runtime, so every Android application can create its own SQLite databases.


1 Answers

Order by id DESC Limit 1:

db.query("table", null, "column=?", new String[]{"value"}, null, null, "id DESC", "1"); 
like image 126
Plamen Nikolov Avatar answered Sep 30 '22 18:09

Plamen Nikolov