android-实例2-数据库sqlite(查询)

调用 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;
   }
相关推荐
菜鸟~noob2333 分钟前
【电子战】第三季预告—波武器与电子系统强力毁伤
开发语言·数据库·matlab·电子战
三少爷的鞋1 小时前
AI 时代,我们都将成为通才型开发者:只懂 Android,已经不够了
android
Seraphina362 小时前
SQL注入(二)盲注
网络·数据库·经验分享·笔记·sql
盟接之桥3 小时前
AI助手:你的7×24小时智能管家——让异常预警无处不在
大数据·网络·数据库·人工智能·制造·ai编程
waoooqwe3 小时前
问卷样本真实吗
大数据·数据库·算法·数据分析
可乐ea4 小时前
Agent 评测换判据:用终态数据库状态判定任务是否完成
数据库·ui·oracle·mcp工具·agent评测·终态断言·智能体可靠性
l1t4 小时前
DeepSeek总结的一个不含数据的 DuckDB 数据库
服务器·数据库·duckdb
PaperData5 小时前
2007-2020年税收调查面板数据
数据库
Dovis(誓平步青云)5 小时前
家里设备越来越多,如何用一张空间地图控制灯光和温度![
android·java·前端·javascript·人工智能·电脑
程序员清风5 小时前
CSV、Excel 与数据库数据读取实践
数据库·oracle·excel