【问题标题】:Fetching data from sqlite database based on timestamp of present day根据当前日期的时间戳从 sqlite 数据库中获取数据
【发布时间】:2019-05-31 22:49:45
【问题描述】:

在我的 android sqlite 数据库中,有一列包含数据格式 (2013-12-12 18:47:31) 的时间戳。现在,特定数据可能具有早于当前或晚于当前的时间戳。例如今天是 2013 年 12 月 20 日,数据库包含 2013 年 11 月 1 日、2013 年 11 月 6 日、2013 年 12 月 12 日、2014 年 1 月 1 日等的数据......现在我想获取早于现在的数据,即 2013 年 12 月 20 日。怎么办那个?

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    您可以使用以下查询根据时间戳获取数据

    SELECT [COLUMN 1], [COLUMN 2]...., [COLUMN N] FROM [TABLE] WHERE TIMESTAMP_FIELD < strftime('%s','now');
    

    - Date And Time Functions

    否则你也可以像这样获取 currentTimeStamp

    SELECT [COLUMN 1], [COLUMN 2]...., [COLUMN N] FROM [TABLE] WHERE TIMESTAMP_FIELD <
    CAST((((JulianDay('now', 'localtime') - 2440587.5)*86400.0) + 62135596800) * 10000000
    AS BIGINT)
    

    - Link for reference

    【讨论】:

      【解决方案2】:
      select [Columns] from [database] where [column_date] < "currentDate";
      

      要获取当前日期和时间,请查看此链接Get current time and date on Android

      【讨论】:

        【解决方案3】:

        yyyy-MM-dd HH:mm:ss 日期时间戳具有相同的字典顺序和时间顺序。您可以在字符串上使用&lt;&gt; 等运算符。

        【讨论】:

          【解决方案4】:
          Time time = new Time();
          
          time.setToNow();
          

          Date todayDate = new Date();
          
          todayDate.getDay();
          
          todayDate.getHours();
          
          todayDate.getMinutes();
          
          todayDate.getMonth();
          
          todayDate.getTime();
          

          【讨论】:

            【解决方案5】:

            我遇到了使用时间戳获取数据的问题,但几周后我找到了一个好的解决方案,但无济于事。

            1. 将列时间戳设置为表格上的文本
             public static final String CREATE_TABLE =
                        "CREATE TABLE " + TABLE_NAME + "("
                                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                                + COLUMN_INCOME + " INTEGER,"
                                + COLUMN_EXPENSE + " INTEGER,"
                                + COLUMN_TIMESTAMP + " TEXT"
                                + ")";
            
            1. 使用 new Date() 获取日期并转换为字符串
             private String getDateTime() {
                    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                    Date date = new Date();
                    return dateFormat.format(date);
                }
            
            1. 使用 getDateTime() 作为时间戳在表中插入数据
            ContentValues values = new ContentValues();
            
                    values.put(DailyTransaction.COLUMN_INCOME, income);
                    values.put(DailyTransaction.COLUMN_EXPENSE, expense);
                    values.put(DailyTransaction.COLUMN_TIMESTAMP, getDateTime());
            
            1. 那么您的 sqlite 查询将如下所示:
            "SELECT * FROM "+ TABLE_NAME + " WHERE " + ClassName.COLUMN_TIMESTAMP + " <"+ "'"currentDate'";
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-03-07
              • 2020-01-23
              • 2023-01-24
              • 1970-01-01
              • 2015-11-19
              • 2019-11-07
              • 2013-03-06
              • 2019-05-21
              相关资源
              最近更新 更多