malli
malli

Reputation: 640

how to query the sqlite using limit

I write the selection query like this:

'String selection3 = TriplePayProviderMetaData.ProgramTableMetaData.PROGRAM_LCN + " = '" + channelLcn + "'" + " AND " + TriplePayProviderMetaData.ProgramTableMetaData.PROGRAM_START_TIME + " > " + " '" + time + "'" + " LIMIT " + " '" + 3 + "'";'

But it says syntax error at limit:

'android.database.sqlite.SQLiteException: near "LIMIT": syntax error: , while compiling: SELECT title, lcn, _id, end_time, description, program_logo, start_time, rating, parent_guide FROM programs WHERE (lcn = '100' AND start_time > '1327026600000' LIMIT '3') ORDER BY start_time ASC E/AndroidRuntime(15912): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158) E/AndroidRuntime(15912): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114) E/AndroidRuntime(15912): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330) E/AndroidRuntime(15912): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366) E/AndroidRuntime(15912): at android.content.ContentProviderClient.query(ContentProviderClient.java:49) E/AndroidRuntime(15912): at com.vovinet.miniepguru.MiniEpgActivity$1.onClick(MiniEpgActivity.java:150) E/AndroidRuntime(15912): at android.view.View.performClick(View.java:2485) E/AndroidRuntime(15912): at android.view.View.onKeyUp(View.java:4257) E/AndroidRuntime(15912):
at android.view.KeyEvent.dispatch(KeyEvent.java:1280) E/AndroidRuntime(15912): at android.view.View.dispatchKeyE W/ActivityManager( 1133): Force finishing activity com.vovinet.miniepguru/.MiniEpgActivity W/ActivityManager( 1133): Activity pause timeout for HistoryRecord{40630af0 com.vovinet.miniepguru/.MiniEpgActivity}'

Upvotes: 0

Views: 4089

Answers (2)

Arslan Anwar
Arslan Anwar

Reputation: 18746

You are passing limit as String. Limit is an integer value. So remove "'" tag from limit.

Also Limit has two parameters

LIMIT 0 , 10

1st value 0 show that start getting record form 0 position. 2nd value 10 show that get then record from starting position. So it will return 1st 10 records.

if you say 10 , 10. It will get 10-20 record form the cursor. As starting position is 10 and no of record is also 10.

You can also check this link http://www.sqlite.org/lang_select.html

Edited: Your query is

SELECT title, lcn, _id, end_time, description, program_logo, start_time, rating, parent_guide FROM programs WHERE (lcn = '100' AND start_time > '1327026600000' LIMIT 0 , 3) ORDER BY start_time ASC

Basically you are adding limit wrongly

LIMIT is used after ORDER BY cluse ( check the link http://www.sqlite.org/lang_select.html)

It should be

SELECT title, lcn, _id, end_time, description, program_logo, start_time, rating, parent_guide FROM programs WHERE (lcn = '100' AND start_time > '1327026600000') ORDER BY start_time ASC LIMIT 0 , 3

Upvotes: 2

Hubert
Hubert

Reputation: 1

In fact the SQLiteDatabase has a function to do this thing.

public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) Query the given table, returning a Cursor over the result set.

limit Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.

Please look at here

Upvotes: 0

Related Questions