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;
   }
相关推荐
IT19956 小时前
Fail2Ban 防护 MySQL 实战|登录失败 3 次,IP 封禁 24 小时
数据库·mysql
夕除6 小时前
redis--IO 多路复用(select + poll)
数据库·sql
警醒与鞭策9 小时前
Codex桌面版与Claude桌面版使用设置
android·unity·ai·性能优化
lzhdim10 小时前
100条常用SQL语句
数据库·sql·mysql·mybatis
天河归来10 小时前
从原始消息到业务数据:三层数据架构的设计与实践
数据库·数据仓库·架构
数据库小学妹10 小时前
金融数据库成本怎么算?五笔钱加一张TCO表
数据库·金融行业·数据库选型·国产化替代·tco·金融数据库
KaiwuDB10 小时前
KaiwuDB 分布式集群扩缩容,从原理到完整操作踩坑总结
运维·数据库·分布式
auspicious航10 小时前
PostgreSQL的备份与恢复
数据库·postgresql
这个DBA有点耶11 小时前
从 MySQL 到时序数据库:我帮朋友做迁移的完整记录
数据库·架构·dba
泡海椒11 小时前
深度解析JQuick-SQL AST到逻辑计划转换机制|分布式SQL原理
数据库·分布式·sql