Android中 数据库操作 like 和 limit 的写法

like怎么用可以去w3c自己搞。

但有几个问题是需要知道的。

1. like 怎么写

    因为string变量可以包含需要转义的字符,如果不转义直接拼sql必然会使sql失效。自己做检查太繁琐。一般采用?的形式。

    直接贴代码了。

    private String getSelection(String columnName) {
        return columnName + " like ? ";
    }

    private String getOrder(String columnName) {
        return columnName + "ASC";
    }

    private List<String> searchListEx(String text) {
        final String[] columnNames = new String[] {PushMedicine.Tabcol_NameCn, PushMedicine.Tabcol_NameEn};
        final String[] selectionArgs = new String[] { "%" + text + "%"};
        final List<String> result = new LinkedList<String>();
        for (String columnName : columnNames) {
            final String selection = getSelection(columnName);
            final String order = getOrder(columnName);
            Cursor cursor = getContentResolver().query(PushMedicine.URI, new String[]{columnName},selection,selectionArgs,order);
            if (cursor == null || cursor.getCount() == 0) continue;
            List<String> list = parseCursor(cursor);
            if (!Util.isNull(list)) result.addAll(list);
        }
        return result;
    }

2. limit怎么写。

    放在orderBy之后

@Override
        Recorder onFindBeforeOne(long timeMills) {
            String whereClause = Recorder.TABCOL_RECORDTIME + " < ? ";
            String[] whereArgs = new String[]{
                    timeMills + "",
            };
            String orderBy = Recorder.TABCOL_RECORDTIME + " desc " + "LIMIT 1";
            Cursor c = mResolver.query(Recorder.URI, Recorder.TABCOLS, whereClause,
                    whereArgs, orderBy);
            List<Recorder> recorders = Recorder.GetData.getDatas(c);
            if (c != null) {
                c.close();
            }
            return recorders == null ? null : recorders.get(0);
        }

 

   

posted @ 2013-07-30 11:11  wFeng  阅读(941)  评论(0编辑  收藏  举报