开发者

android SQLite help select last 10

hello i use this code for diplay my conversation list

private Cursor getMessages( SQLiteDatabase db, boolean justNewMessages) {


    final String table = Constants.TABLE_CONVERSATION;
                                                                            //index
    final String[] columns = { Constants.TABLE_CONVERSATION_FIELD_ID,       //0
                               Constants.TABLE_CONVERSATION_FIELD_DATE,     //1
                               Constants.TABLE_CONVERSATION_FIELD_FROM,     //2
                               Constants.TABLE_CONVERSATION_FIELD_TO,       //3
                               Constants.TABLE_CONVERSATION_FIELD_MSG       //开发者_运维问答4
        };

    String selection;

    if(justNewMessages) {
        selection = "(" + Constants.TABLE_CONVERSATION_FIELD_FROM + " = '" + jid +"' or "
                        + Constants.TABLE_CONVERSATION_FIELD_TO + " = '" + jid + "') and "
                        + Constants.TABLE_CONVERSATION_FIELD_NEW + " = '1'";
    } else {
        selection = "(" + Constants.TABLE_CONVERSATION_FIELD_FROM + " = '" + jid +"' or "
                        + Constants.TABLE_CONVERSATION_FIELD_TO + " = '" + jid + "')";
    }

    final String[] selectionArgs = null;
    final String groupBy = null;
    final String having = null;
    final String orderBy = Constants.TABLE_CONVERSATION_FIELD_DATE;

    return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}

I would like select 10 last i try

final String orderBy = Constants.TABLE_CONVERSATION_FIELD_DATE + " DESC LIMIT 10";

it is the last conversation, but reversed(the last word is up) how Select only 10 last conversation OR delete and leaves the 10 last conversation and display this ... Cant you help me to start idea I do not understand ...


try out this query,

Select * from [table] LIMIT 
((Select count([any_column_name]) from [table])-10),10

this will fetch last 10 rows of your table. replace *any_column_name* with any Column of yr table.

Regards, Nital Shah


FIXED Thanks for the tips it's the solution, thanks thanks thanks

Fixed by

final String tableff ="Select "+Constants.TABLE_CONVERSATION_FIELD_ID+","+Constants.TABLE_CONVERSATION_FIELD_DATE+","+Constants.TABLE_CONVERSATION_FIELD_FROM+","+Constants.TABLE_CONVERSATION_FIELD_TO+","+Constants.TABLE_CONVERSATION_FIELD_MSG+" from ["+table+"] WHERE "+selection+" LIMIT ((Select count(["+Constants.TABLE_CONVERSATION_FIELD_DATE+"]) from ["+table+"] WHERE "+selection+")-10),10";

    return db.rawQuery(tableff, selectionArgs);


try this:

final String orderBy = Constants.TABLE_CONVERSATION_FIELD_DATE + " DESC";
final String limit= 10;

return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜