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;
   }
相关推荐
海兰1 小时前
【数据库】告别数据库性能噩梦,拥抱 UUID v7
数据库
linweidong1 小时前
字节AI Agent面试题大全及参考答案(下)
数据库·oracle
时代分流1 小时前
云手机推荐2026:安卓全能、批量挂机、原生iOS全覆盖
android·ios·智能手机
恋猫de小郭1 小时前
Gradle 9.7.0 将提速 Android 构建,Sync 提升接近一倍
android·前端·flutter
枫彩1 小时前
WorkBuddy 做A股自动复盘实战:数据校验、工具调用与定时报告
大数据·网络·数据库·a股·mcp·workbuddy
其实防守也摸鱼1 小时前
CTF 入门到进阶:Web、逆向、密码学与 Pwn 实战解题指南
数据库·安全·自动化·github·copilot
晓子文集1 小时前
Tushare接口文档:每日停复牌信息(suspend_d)
大数据·数据库·金融·金融数据·量化投资
Dovis(誓平步青云)2 小时前
心情日记的安全编辑流程:输入校验、删除确认与状态回收
android·运维·服务器·开发语言·windows·安全
ggabb2 小时前
九九乘法表“西行记”:一场文明底层逻辑的碰撞与启示
sqlite