带字符串的数据库查询,Android

时间:2012-10-29 12:17:16

标签: android android-sqlite

我有一个数据库表,用户可以在其中保存一些字符串。我想比较包含日期的两个字符串之间的差异,数字也是字符串。更具体一点:两个日期遵循这种格式,例如“2012-10-24 00:00”,数字就像这个“2”。有没有办法在查询中区分两个日期?我也尝试将日期格式化为long,但由于通过整个数据库的大循环,我得到了强制关闭。提前谢谢你

mCursor1.moveToFirst();
                mCursor1= mDbHelper.fetchTasks();

                     for(int u=0; u<mCursor2.getCount(); u++){

                   mCursor1.moveToPosition(u);
                  long id=mCursor1.getLong(0);


                Date Date1 = ConvertToDate(this.mCursor1.getString(5));
                 Date DateNow = ConvertToDate( current );
                 Calendar a= DateToCalender (this.mCursor1.getString(5));
                 Calendar b= DateToCalender(current);
                 a.add(Calendar.MONTH, 1);
                 b.add(Calendar.MONTH, 1);



                           long diff= daysBetween(b,a);
                            if(dateNotify=="1"&& diff%1==0){
                                 mDbHelper.updateNotification(id,"true");

                                    }
                                 else if(dateNotify=="2"&& diff%2==0){
                                      mDbHelper.updateNotification(id,"true");

                                      }
                                 else if(dateNotify=="3"&& diff%3==0){
                                         mDbHelper.updateNotification(id,"true");

                                      }
                                 else      if(dateNotify=="7"&& diff%7==0){
                                         mDbHelper.updateNotification(id,"true");

                                      }



              mCursor1.moveToNext();
                 }



          }

/ 从日期转换为日历 /             公共日历DateToCalender(String str_date){

              SimpleDateFormat  formatter ; 
              Date date ; 
              Calendar cal;
              formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
              try {
                 date = (Date)formatter.parse(str_date);
                 cal=Calendar.getInstance();
                 cal.setTime(date);
                 } catch (ParseException e) {
                 e.printStackTrace();
                return null;
            } 
          return cal;
         }




        /*Days difference between two dates*/
        public static long daysBetween(Calendar startDate, Calendar endDate) {
              Calendar date = (Calendar) startDate.clone();
              long daysBetween = 0;
              while (date.before(endDate)) {
              date.add(Calendar.DAY_OF_MONTH, 1);
              daysBetween++;
               }
              return daysBetween;
         }


        /*Convert string to Date*/
       public Date ConvertToDate(String dateString){

                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

                Date convertedDate;
                try {

                    convertedDate = dateFormat.parse(dateString);


                } catch (ParseException e) {
                    e.printStackTrace();
                    return null;
                }

                return convertedDate;

         }

1 个答案:

答案 0 :(得分:0)

尝试此查询。

return db.query(TABLE_NAME, new String[]{"temp_sp_date","temp_sp_amount"},
                        "(temp_sp_user_id = ?) and (temp_sp_date >= ? and temp_sp_date <= ? )" , new String[] {String.valueOf(userId),startDate,endDate}, " strftime(" + "\"%Y" + "-" + "%m" + "-" + "%d\""  + ",temp_sp_date) ",null , null);

根据您的要求更改字段和值。