我的查询不适用于datetime请帮助我

时间:2014-02-26 09:22:19

标签: android

我希望使用当前日期从数据库获取最近1周的记录但我的查询将不会从数据库获得任何结果帮助我我尝试这么多看屏幕截图

enter image description here

数据库中存储的日期是“datetime”格式和我使用当前日期1周龄日期的日期是字符串格式有什么问题请帮帮我

  value save in database in this format  " 19/02/2014   14:08"
   jobarr = db.getALLCompJobs(sCurrent,sWeekBefore);



        private static final String TABLE_COMPLETED_JOBS = "jobs";


    private static final String KEY_COMPID = "_compid";
private static final String KEY_TIMEJOB = "timejob";
private static final String KEY_TIMEWEEK = "timeweek";

private static final String KEY_PICK = "pick";
private static final String KEY_DEST = "dest";
private static final String KEY_FARE = "fare";

        String CREATE_COMPLETED_TABLE = "CREATE TABLE " + TABLE_COMPLETED_JOBS
            + "(" + KEY_COMPID + " INTEGER PRIMARY KEY," + KEY_TIMEJOB
            + " TEXT,"   + KEY_PICK + " TEXT," + KEY_DEST + " TEXT,"
            + KEY_FARE + " TEXT,"
            + KEY_TIMEWEEK + " datetime" + ")";



        public List<JobSchmeModel> getALLCompJobs(String sCurrent,String sWeekBefore) {
    List<JobSchmeModel> compjobsList = new ArrayList<JobSchmeModel>();


        String selectQuery = "SELECT  * FROM " + TABLE_COMPLETED_JOBS + " WHERE " 
+ KEY_TIMEWEEK + ">=" + sWeekBefore + " AND " + KEY_TIMEWEEK + "<=" + sCurrent + " 
 ORDER BY "+KEY_COMPID+" DESC";

     SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);


    when i see in debug query look like this

  SELECT  * FROM jobs WHERE timeweek>=19/02/2014   09:17 AND timeweek<=26/02/2014   
 09:17 ORDER BY _compid DESC

1 个答案:

答案 0 :(得分:0)

你需要在这里引用你的文字:

SELECT  * FROM jobs WHERE timeweek>=19/02/2014   09:17 AND timeweek<=26/02/2014 
像这样

SELECT  * FROM jobs WHERE timeweek>='19/02/2014   09:17' AND timeweek<='26/02/2014'

这将修复语法。但是,它将使用词典(按字母顺序)排序而不是按时间顺序排序,因为数据实际上只是字符串,而不是日期时间戳。

要使其按照您希望的方式运行,您需要更改存储的数据,以便排序符合时间顺序。例如,使用

  1. Unix时间戳(自纪元以来的秒数)或Java时间戳(自纪元以来的毫秒数)
  2. ISO 8601邮票,如YYYY-MM-DD'T'HH:MM
  3. 最简单的方法可能是使用SimpleDateFormat 解析服务器返回的日期,并将Java毫秒时间戳存储在数据库中。