简体   繁体   English

如何从sqlite数据库中以dd / mm / yyyy格式获取两个日期之间的数据

[英]How to get data between two dates from sqlite database which is in dd/mm/yyyy format

I'm building an app which select rows between to dates which is in dd/mm/yyyy format and count the row having status pending, signup and rejected. 我正在构建一个应用程序,该应用程序可以选择日期之间的行,日期格式为dd / mm / yyyy,并计算状态为未决,注册和拒绝的行。 I have done some work on it, but its not working. 我已经做了一些工作,但是没有用。 I stored date as TEXT in database. 我将日期作为TEXT存储在数据库中。 I pasted code below. 我在下面粘贴了代码。

public void showMonthlyPopUp(View view) {
    weeklyDialog.setContentView(R.layout.pop_up_all_list);
    TextView nameTextView = weeklyDialog.findViewById(R.id.textView);
    nameTextView.setText("MONTHLY");
    TextView pendingTextView = weeklyDialog.findViewById(R.id.textView6);
    TextView signUpTextView = weeklyDialog.findViewById(R.id.textView3);
    TextView rejectedTextView = weeklyDialog.findViewById(R.id.textView7);
    Button shareButton = weeklyDialog.findViewById(R.id.button_share);

    String[] projection = {
            InfoContract.InfoEntry._ID,
            InfoContract.InfoEntry.COLUMN_STATUS,
            InfoContract.InfoEntry.COLUMN_DATE
    };

    Calendar calendar = Calendar.getInstance();
    String strDate = calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
    int dayInt = calendar.get(Calendar.DAY_OF_MONTH);

    String[] selectionArgs = new String[dayInt];
    for (int i = 1; i <= dayInt; i++) {
        selectionArgs[i - 1] = i + "/" + strDate;
    }

    String selection = InfoContract.InfoEntry.COLUMN_DATE + " =?";
    for (int i = 1; i < dayInt; i++) {
        selection += " OR " + InfoContract.InfoEntry.COLUMN_DATE + " =?";
    }
    Cursor cursor = this.getContentResolver().query(InfoContract.InfoEntry.CONTENT_URI, projection, selection, selectionArgs, null);
    int pending = 0;
    int signUp = 0;
    int rejected = 0;
    while (cursor.moveToNext()) {
        int statusColumnIndex = cursor.getColumnIndex(InfoContract.InfoEntry.COLUMN_STATUS);
        int status = cursor.getInt(statusColumnIndex);
        if (status == InfoContract.InfoEntry.STATUS_SIGN_UP) signUp = signUp + 1;
        else if (status == InfoContract.InfoEntry.STATUS_REJECTED) rejected++;
        else pending++;
    }
    cursor.close();
    pendingTextView.setText("" + pending);
    signUpTextView.setText("" + signUp);
    rejectedTextView.setText("" + rejected);
    weeklyDialog.show();
    final int finalPending = pending;
    final int finalSignUp = signUp;
    final int finalRejected = rejected;
    shareButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            shareData(finalPending, finalSignUp, finalRejected, "Monthly Details: ");

        }
    });

}

Life will be quite hard using the format dd/mm/yyyy as it's not readily usable by the most obvious SELECT using a BETWEEN clause as part of the WHERE clause, even harder when dd/mm/yyyy is often with single characters for values that are less than 10 (eg 1/1/2019 instead of 10/10/2019). 使用dd / mm / yyyy格式的日子将非常艰辛,因为使用BETWEEN子句作为WHERE子句的一部分时,最明显的SELECT并不容易使用它,当dd / mm / yyyy经常使用单个字符表示值时,甚至更难小于10(例如1/1/2019而不是10/10/2019)。

Consider mytable created and loaded using :- 考虑使用以下命令创建和加载mytable

DROP TABLE IF EXISTS mytable;
CREATE TABLE IF NOT EXISTS mytable (mydatecolumn TEXT, myothercolumn TEXT DEFAULT 'BLAH');
INSERT INTO mytable (mydatecolumn) 
    VALUES 
        ('01/01/2019'),('1/1/2019'),('01/1/2019'),('1/01/2019'),
        ('10/1/2019'),('10/10/2019'),('1/10/2019'),('01/02/2019'),
        ('1/3/2019'),('01/1/2019'),('14/01/2019'),('10/1/2019'),
        ('10/10/2020'),('1/10/2018')
; 

Which looks like :- 看起来像:-

在此处输入图片说明

The query to transpose values and to then select a date range could be :- 用于转置值然后选择日期范围的查询可能是:-

-- An example that would hanlde dd/mm/yyyy where dd and mm could be either 1 or 2 characters
WITH 

    -- First CTE gets the day and the rest of the date
    ctedaypart AS (
        SELECT 
            rowid AS daypartid, 
            substr(mydatecolumn,1,instr(mydatecolumn,'/')-1) AS day_part, 
            substr(mydatecolumn,instr(mydatecolumn,'/')+1) AS rest_after_day 
        FROM mytable
    ),

    -- Second CTE gets the month and the rest of the date
    ctemonthpart AS (
        SELECT 
            daypartid AS monthpartid,
            substr(rest_after_day,1,instr(rest_after_day,'/')-1) AS month_part, 
            substr(rest_after_day,instr(rest_after_day,'/')+1) AS year 
        FROM ctedaypart
    ),

    -- Third CTE expands the day and month the have a leading 0 id less than 10 and joins the parts to form YYYY-MM-DD 
    expandedparts AS (
        SELECT
            *,
            mytable.rowid AS expandedpartsid,
          year||'-'||
            CASE WHEN length(month_part) = 1 THEN '0'||month_part ELSE month_part END ||'-'||
            CASE WHEN length(day_part) = 1 THEN '0'||day_part ELSE day_part END AS date_in_sqlite_format
        FROM mytable JOIN ctedaypart ON mytable.rowid = daypartid  JOIN ctemonthpart ON daypartid = monthpartid)

SELECT mytable.* FROM mytable JOIN expandedparts ON mytable.rowid = expandedpartsid WHERE (date_in_sqlite_format) BETWEEN ('2019-01-01') AND ('2019-03-31');

The above results in 10 of the 14 rows being selected as per :- 上面的结果是按以下方式选择了14行中的10行:

在此处输入图片说明


However 然而

If the date is saved in the database in a recognised format eg YYYY-MM-DD then the above could simply be :- 如果日期以公认的格式(例如YYYY-MM-DD)保存在数据库中,则上述内容可能只是:-

SELECT * FROM mytable WHERE (mydatecolumn) BETWEEN ('2019-01-01') AND ('2019-03-31');

As such it is suggested that you adopt the use of a recognised format for dates when interacting with the database :- 因此,建议您在与数据库交互时对日期使用公认的格式 :-

 Time Strings A time string can be in any of the following formats: YYYY-MM-DD YYYY-MM-DD HH:MM YYYY-MM-DD HH:MM:SS YYYY-MM-DD HH:MM:SS.SSS YYYY-MM-DDTHH:MM YYYY-MM-DDTHH:MM:SS YYYY-MM-DDTHH:MM:SS.SSS HH:MM HH:MM:SS HH:MM:SS.SSS now DDDDDDDDDD 

SQL As Understood By SQLite - Date And Time Functions SQLite理解的SQL-日期和时间函数

The alternative is to use or adapt the complex query above or use one that is similar. 另一种选择是使用或改编上面的复杂查询,或使用类似的查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在sqlite数据库中插入数据类型日期(yyyy-MM-dd)并在android中的两个日期之间检索数据 - how to insert data type date(yyyy-MM-dd) in sqlite database and retrive data between two dates in android JAVA控制台应用程序以MM / DD / YYYY格式计算两个用户指定的日期之间的持续时间 - JAVA console app to calculate the duration between two user specified dates in MM/DD/YYYY format 如何以“ yyyy-mm-dd”格式将日期列表放入JComboBox? - How to put list of dates in JComboBox in 'yyyy-mm-dd' format? 如何使用Java将mm-dd-yyyy(来自csv文件中的数据)的格式更改为yyyy-mm-dd存储在MySql中 - How to change format of mm-dd-yyyy(of data from csv file) to be stored as yyyy-mm-dd in MySql using Java 数据库中的日期格式为yyyy / MM / dd,我希望它来自MM / dd / yyyy - Date format in Database is in yyyy/MM/dd i want it in the from MM/dd/yyyy 如何使用YYYY-MM-DD格式让SQLite按日期排序? - How can i get SQLite to sort by date properly using YYYY-MM-DD format? 如何以'dd-mm-yyyy'和'yyyy-yyyy'格式SWING在JComboBox中放置日期列表? - How to put list of dates in JComboBox in 'dd-mm-yyyy' and also in 'yyyy-yyyy' format SWING? 我需要一个代码,该代码应使用Java从Excel获取YYYY-MM-DD格式的日期 - I need a code which should get Date from Excel as YYYY-MM-DD format using java 如何防止FastDateFormat模式“yyyy-MM-dd”以“dd-MM-yyyy”格式解析String - How to prevent FastDateFormat pattern “yyyy-MM-dd” from parsing String in format “dd-MM-yyyy” 如何以yyyy-MM-dd HH:mm:ss格式从Oracle数据库检索Date变量并以相同格式存储在Date变量中? - How to retrieve the Date variable from oracle database in yyyy-MM-dd HH:mm:ss format and store in Date variable in the same format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM