SQLite查询执行条件错误

时间:2016-02-06 05:02:53

标签: android sqlite

SQLite查询没有显示正确的答案。此查询中的错误是什么。

 "SELECT DISTINCT bike_reg_number from bookings WHERE vendor_id = '"+id+"' AND "+
            "bike_reg_number = '"+temp+"' AND (( from_date > '"+from_dt+"' AND from_date > '"+
             todt+"') OR "+ "(to_date < '"+from_dt+"' AND to_date < '"+todt+"'))";

我的条件是,

(vendor_id must be there) and 
(bike_reg_number must be there) and
(from_date must be > the given both input from_dt and todt) or
(to_date must be < the given both input from_dt and todt)

修改:

许多可用于特定车辆的预订(AA-01-1000)。让我们说,

(2016-02-06 to 2016-02-06),
(2016-02-11 to 2016-02-11),
(2016-02-15 to 2016-02-16),
(2016-02-19 to 2016-02-20)

我想知道(AA-01-1000)车辆是否可用(2016-02-08至2016-02-09)。现在它将正确返回。如果我给(2016-02-17至2016-02-19)它显示错误。为什么因为只有一个记录是错误的。因此,另一个记录真实值显示车辆编号。

实际上,如果预订表中的任何一个条件失败,我不想显示车辆名称。那么,只有sqlite这可能吗?花了更多时间来修复这个bug。仍然没有修复。

1 个答案:

答案 0 :(得分:0)

"SELECT DISTINCT bike_reg_number from bookings WHERE vendor_id = '" + id +
"' AND bike_reg_number = '" + temp + "'";

检索数据并在java中进行操作以进行日期比较。只有那个查询我不能这样做,所以我决定用java代码进行日期比较。

 Cursor cursor1 = db.rawQuery(selectQuery1, null);
    if (cursor1.moveToFirst()) {
        String[] vehicle_number=new String[cursor1.getCount()];
        String[] from_date=new String[cursor1.getCount()];
        String[] to_date=new String[cursor1.getCount()];
        int i=0;
            do {
            vehicle_number[i]=(cursor1.getString(0));
            from_date[i]=(cursor1.getString(1));//dummy
            to_date[i]=(cursor1.getString(2));//dummy
            i++;
            } while (cursor1.moveToNext());
            try {
                String vehi=getDateComparison(vehicle_number,from_date,to_date,from_inp,to_inp);
                VehicleGetSetter contact = new VehicleGetSetter();
                contact.setNumber(vehi);
                contactList1.add(contact);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    } 

    private String getDateComparison(String[] veh, String[] from, String[] to,String from1, String to1) throws ParseException {
       SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);
       Date table_from,table_to,input_from,input_to;
       long diff=0,diff1=0,diff2=0,diff3=0;
       int count=0,county=0;;
       String vehic="";
       for(int i=0;i<veh.length;i++){
          table_from=df.parse(from[i]);
          table_to=df.parse(to[i]);
          input_from=df.parse(from1);
          input_to=df.parse(to1);
          table_from=df.parse(df.format(table_from));
          table_to=df.parse(df.format(table_to));
          input_from=df.parse(df.format(input_from));
          input_to=df.parse(df.format(input_to));
          diff=table_from.getTime()-input_from.getTime();
          diff1=table_from.getTime()-input_to.getTime();
          diff2=input_from.getTime()-table_to.getTime();
          diff3=input_to.getTime()-table_to.getTime();
          if((diff > 0 && diff1>0) || (diff2 > 0 && diff3>0)){
            count++;
            Log.d("Date", " ok with from");
          }
         else
            county=100;
       }
       if(county==100)
          return vehic;
       else
          vehic= veh[0];

      return vehic;
     }