like怎么用可以去w3c自己搞。
但有几个问题是需要知道的。
1. like 怎么写
因为string变量可以包含需要转义的字符,如果不转义直接拼sql必然会使sql失效。自己做检查太繁琐。一般采用?的形式。
直接贴代码了。
private String getSelection(String columnName) { return columnName + " like ? "; } private String getOrder(String columnName) { return columnName + "ASC"; } private ListsearchListEx(String text) { final String[] columnNames = new String[] {PushMedicine.Tabcol_NameCn, PushMedicine.Tabcol_NameEn}; final String[] selectionArgs = new String[] { "%" + text + "%"}; final List result = new LinkedList (); 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 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); Listrecorders = Recorder.GetData.getDatas(c); if (c != null) { c.close(); } return recorders == null ? null : recorders.get(0); }