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;
   }
相关推荐
恋猫de小郭3 小时前
Flutter 3.50.0 Release 的第一个崩溃,修好了,还没 hotfix
android·前端·flutter
隔窗听雨眠12 小时前
SQL执行计划固化的原理与稳定性工程
数据库·sql优化·执行计划
alexhilton13 小时前
MVI 让屏幕一次只有一个状态
android·kotlin·android jetpack
TDengine (老段)14 小时前
TDengine IDMP 功能点—数据接入
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
索西引擎14 小时前
MySQL 进阶:视图、存储过程、存储函数与触发器
数据库·mysql
bksczm16 小时前
InnoDB 通用表空间与临时表空间详解
大数据·数据库·mysql
靠沿16 小时前
MySQL数据库进阶——存储过程和触发器
java·数据库·mysql
码少女17 小时前
MySQL--库与表的增删改查基础
java·数据库·mysql
Eyfcom17 小时前
〖物流 SaaS 实战 02〗SaaS 租户的计费与订阅模型:从“一锤子买卖”到“按需订阅”
linux·服务器·数据库·快递系统·物流saas实战
打工的小王18 小时前
postgresql和mysql对比
数据库·mysql·postgresql