簡體   English   中英

如何從sqlite數據庫中以dd / mm / yyyy格式獲取兩個日期之間的數據

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

我正在構建一個應用程序,該應用程序可以選擇日期之間的行,日期格式為dd / mm / yyyy,並計算狀態為未決,注冊和拒絕的行。 我已經做了一些工作,但是沒有用。 我將日期作為TEXT存儲在數據庫中。 我在下面粘貼了代碼。

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: ");

        }
    });

}

使用dd / mm / yyyy格式的日子將非常艱辛,因為使用BETWEEN子句作為WHERE子句的一部分時,最明顯的SELECT並不容易使用它,當dd / mm / yyyy經常使用單個字符表示值時,甚至更難小於10(例如1/1/2019而不是10/10/2019)。

考慮使用以下命令創建和加載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')
; 

看起來像:-

在此處輸入圖片說明

用於轉置值然后選擇日期范圍的查詢可能是:-

-- 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');

上面的結果是按以下方式選擇了14行中的10行:

在此處輸入圖片說明


然而

如果日期以公認的格式(例如YYYY-MM-DD)保存在數據庫中,則上述內容可能只是:-

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

因此,建議您在與數據庫交互時對日期使用公認的格式 :-

 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 

SQLite理解的SQL-日期和時間函數

另一種選擇是使用或改編上面的復雜查詢,或使用類似的查詢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM