【问题标题】:Compare ZonedDatetime to delete records in SQLite?比较 ZonedDatetime 以删除 SQLite 中的记录?
【发布时间】:2020-12-18 15:51:43
【问题描述】:

问题: 我在 SQLITE DB 中有一个存储 ZonedDateTime 的列。我需要使用 ZonedDateTime 时间戳列删除早于特定日期的记录。

到目前为止完成:

  • 尝试查询记录并从中提取LocalDate 以比较日期早于特定日期。
  • 但是,这只会创建LocalDate 而不是ZonedDateTime 的列表,以便我可以比较这些循环的ZonedDateTime 以删除记录。感谢您的帮助!

【问题讨论】:

    标签: android sqlite localdate zoneddatetime localdatetime


    【解决方案1】:

    解决了问题

    public ArrayList<String> CompareZDT(String st) {
    
        ArrayList<String> ZDTtodelete;
        ArrayList<String> ZDTrejected;
        ZDTtodelete = new ArrayList<String>();
        ZDTtoavoid = new ArrayList<String>();
    
        LocalDate todaysDate = LocalDate.now(ZoneId.of("Asia/Riyadh"));
        LocalDate checkDate = todaysDate.minusDays(120);
    
        String selectQuery = "SELECT * FROM " + Tablename + ";";
    
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        Cursor cursor = database.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
    
                if(ZonedDateTime.parse(cursor.getString(5)).toLocalDate().isBefore(checkDate)) {
                    ZDTtodelete.add(cursor.getString(7));
                }else
                {
                    ZDTrejected.add(cursor.getString(7));
                }
    
            } while (cursor.moveToNext());
        }
        cursor.close();
    
        if (st.equals("DELETE"))
        {
            return ZDTtodelete;
        }else
        {
            return ZDTrejected;
        }
    
    }
    

    使用arraylist [ZDTtodelete],循环并检查DB中的TIMESTAMP列,从而可以完成删除操作。

    【讨论】:

      猜你喜欢
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多