AndroidStudio中litepal数据库多条件+模糊搜索

备份
模糊查找用关键字like和%
1.where中用and连接,condition用?,后面依次写出来

if(!mymajor.equals("null")){//需要查询分数
           List datas=DataSupport.where("classes=? and location =? and major like ?",
                   myclasses,mylocation,"%"+mymajor+"%").find(App.class);
           Log.d("MainActivity2", "count " + datas.size());
           if(datas.size()==0)
               result="暂时没有数据。";
           else{
               result="省份 年份 专业  科类 批次 分数线";
               for(App data:datas){
                   String temp=data.getClasses()+" "
                           +data.getLocation()+" "
                           +data.getYear()+" "
                           +data.getMajor()+" "
                           +data.getAdmissionBatch()+" "
                           +data.getScore15();
                   result+="\n"+temp;
               }
           }
           for(App data : datas) {
               Log.d("MainActivity2", "num is " + data.getNum());
               Log.d("MainActivity2", "year is " + data.getYear());
               Log.d("MainActivity2", "location is " + data.getLocation());
               Log.d("MainActivity2", "classes is " + data.getClasses());
               Log.d("MainActivity2", "major is " + data.getMajor());
               Log.d("MainActivity2", "AD is " + data.getAdmissionBatch());
               Log.d("MainActivity2", "score is " + data.getScore15());
           }

       }

2.直接用sql语句查询,返回Cursor,再依次读出

 //plan B使用sql语句查询
          Cursor c =DataSupport.findBySQL("select *" +
                   " from App where classes=? and location = ?" +
                   " and major like ?", myclasses,mylocation,"%"+mymajor+"%");
           String loca="null",year="null",classes="null",major="null",ad="null",num="null";
           double score=0.0;
           Log.d("MainActivity2", "String: " +c.getCount()+mylocation+myclasses+mymajor);
           if(c.moveToFirst()==false){
               result="暂时查不到数据。";
           }else{
               result="省份 年份 专业  科类 批次 分数线";
               if(c.moveToFirst()){
                   do{
                       loca=c.getString(c.getColumnIndex("location"));
                       year=c.getString(c.getColumnIndex("year"));
                       major=c.getString(c.getColumnIndex("major"));
                       //classes=c.getString(c.getColumnIndex("classes"));
                       //ad=c.getString(c.getColumnIndex("AdmissionBatch"));
                       score=c.getDouble(c.getColumnIndex("score15"));
                       String temp= loca+" "
                               +year+" "
                               +major+" "
                               +classes+" "
                               +ad+" "
                               +score
                               ;
                       result=result+"\n"+temp;
                       //Log.d("MainActivity2", "result: " +result );
                   }while(c.moveToNext());
               }
           }

           c.close();

 

你可能感兴趣的:(AndroidStudio中litepal数据库多条件+模糊搜索)