调用 read(String str),str 也可能数字字符串,如"12" "102" 如果为数字字符串,为 id productId analyteId 查询
java
public ArrayList<BeanNCLimit> read(String str){
Cursor cursor;
ArrayList<BeanNCLimit> listBeans = new ArrayList<>();
SQLiteDatabase db = MySQLiteHelper.getDatabase(context);
//---------------------------------------
//String selection ="productPath like '%"+ str +"%'" + " or analyteName like '%"+ str +"%'";
String selection;
String[] selectionArgs;
// 判断 str 是否为纯数字字符串
if (isNumeric(str)) {
// 数字字符串 → id 或 productId 精确查询
int num = Integer.parseInt(str);
selection = "id = ? OR productId = ? OR analyteId = ? OR analyteCode = ?";
selectionArgs = new String[]{String.valueOf(num), String.valueOf(num), String.valueOf(num), String.valueOf(num)};
} else {
// 非数字字符串 → 模糊查询
selection = "productPath LIKE ? OR analyteName LIKE ?";
String likePattern = "%" + str + "%";
selectionArgs = new String[]{likePattern, likePattern};
}
cursor = db.query(tabel_name,null, selection, selectionArgs,null,null,null);
//---------------------------------------
while (cursor.moveToNext()){
listBeans.add(BeanSetData(cursor));
}
if(cursor!=null)cursor.close();
//---------------------------------------
return listBeans;
}
java
@SuppressLint("Range")
private BeanNCLimit BeanSetData(Cursor c){
BeanNCLimit bean = new BeanNCLimit();
bean.setId( c.getInt( c.getColumnIndex(id)));
bean.setProductId(c.getInt(c.getColumnIndex(productId)));
bean.setProductPath(c.getString(c.getColumnIndex(productPath)));
bean.setTissue(c.getString(c.getColumnIndex(tissue)));
bean.setAnalyteId(c.getInt(c.getColumnIndex(analyteId)));
bean.setAnalyteName(c.getString(c.getColumnIndex(analyteName)));
bean.setAnalyteCategory(c.getString(c.getColumnIndex(analyteCategory)));
bean.setAnalyteCode(c.getInt(c.getColumnIndex(analyteCode)));
bean.setMrlValue(c.getFloat(c.getColumnIndex(mrlValue)));
bean.setUnit(c.getString(c.getColumnIndex(unit)));
bean.setCurveId(c.getInt(c.getColumnIndex(curveId)));
bean.setStatus(c.getString(c.getColumnIndex(status)));
return bean;
}
判断字符串是否为 数字字符串
java
private boolean isNumeric(String str) {
if (str == null || str.isEmpty()) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}