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;
   }
相关推荐
九皇叔叔19 小时前
【第二章】Redis基础入门:Redis是什么、为什么快以及核心特性详解
数据库·redis·缓存
云和恩墨19 小时前
删库不跑路,回滚极速触达——zData S“旁路日志”持续保护原理深剖
数据库
MetaLite19 小时前
SpringBoot接口分层规范-外网网关内部服务与参数边界
java·数据库·spring boot
Wang's Blog21 小时前
Java框架快速入门: Spring Security+OAuth2之元注解简化权限表达式
java·数据库·spring
天空之城--1 天前
Android逆向安全合规接单指南
android·安全
蓝速科技1 天前
医院导诊 AI 数字人一体机场景适配与落地指南丨蓝速科技
运维·数据库·人工智能·科技·自然语言处理·技术分享
QYR-分析1 天前
重轨受电弓行业深度报告:市场格局、技术迭代与发展前景
大数据·数据库·人工智能
消失的旧时光-19431 天前
Android 系统层扫盲 09:AMS、ATMS、WMS、PMS 到底分别管什么?
android·wms·pms·ams·aosp·atms
泡泡鱼(敲代码中)1 天前
MySQL基础学习笔记:从数据模型到DDL全掌握
开发语言·数据库·笔记·学习·mysql
Godikov1 天前
Android 系统级设备应用踩坑实录:sharedUserId 签名、SDK 授权失败 -4、开机自启与 U 盘 OTA 升级
android