Retrieve Data from Sqllite DB between Two dates android

前端 未结 4 1018
盖世英雄少女心
盖世英雄少女心 2020-12-22 13:17

I retrieve data between two dates some how it get correct result and some how it output empty listview when i select dates with month it work properly but when i select dat

相关标签:
4条回答
  • 2020-12-22 13:18

    Try SELECT * FROM "your table" WHERE date BETWEEN "from date" AND "to date";

    0 讨论(0)
  • 2020-12-22 13:32

    you need to store date inform of yyyy-mm-dd and then use this method simply call getWeekData(start_date, end_date);

    public Object getWeekData(String start_date, String end_date) {
            if (!mDataBase.isOpen())
                openDataBase();
    
            Object data = new Object ();
            // Select All Query
            String query = (String) ("Select * from " + TBL_NAME 
                    + " where " + (COL_DATE + " between '" + start_date + "' AND '" + end_date + "'"));
            Cursor cursor = mDataBase.rawQuery(query, null);
            // looping through all rows and adding to list
    
            if (cursor.moveToFirst()) {
                do {
                    //get data over here
                } while (cursor.moveToNext());
            }
    
            // closing connection
            cursor.close();
            close();
            return data;
        }
    
    0 讨论(0)
  • 2020-12-22 13:32

    we have Two solution for this question

    one is we need to convert date and time into milliseconds, and take long data type in

    Sqllite database and save values(converted date and time). And the write query like "SELECT

    data, start_date, end_date from tablename WHERE start_date > end_date".

    Second way is you need to save start and end date+time(yyy-MM-dd HH:mm:ss) in string format

    in Sqllite database, and you need to query like this,

    "SELECT datetime_start,datetime_end FROM tablename WHERE(( DATETIME(atetime_start) >=

    DATETIME ("+"'"+entry_start_time+"'"+")" +" AND DATETIME(datetime_end) < DATETIME

    ("+"'"+entry_end_time+"'"+")); .

    0 讨论(0)
  • 2020-12-22 13:41

    There are two major solutions. All solutions have in common, that the column containing the date has to be ordered somehow. If this order is destroyed your data is corrupt and your queries cannot return the expected results!

    1. Save your Dates as INTEGER in your database and use a method to map Dates to a number:

    A possible way is to use Date.getTime () to map Dates to numbers, but there are many others. Important is that

    1. equal dates get the same number and
    2. that a date that is after another date gets a bigger number.

    This way ordering will be correct for sure. To achieve this with `Java.util.Date.getTime() you only have to set the time to 0:00:00:000 if you want to store date only.

    For example:

    "CREATE TABLE " + Food_TABLE +"(" +
        EX_RowID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
        EX_Cattype + " TEXT NOT NULL, " +
        EX_Date + " INTEGER NOT NULL," +
        EX_Price + " INTEGER NOT NULL," +
        EX_Type + " TEXT NOT NULL UNIQUE );"
    
    private static String dateOnly(java.util.Date d) {
        Calendar cal = Calendar.getInstance(); // locale-specific
        cal.setTime(d);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return Long.toString(cal.getTimeInMillis());
    }
    
    public Cursor CstmRpot(java.util.Date fd, java.util.Date td) {
        // TODO Auto-generated method stub
        String[] columns = new String[]{EX_RowID,EX_Cattype, EX_Date, EX_Price, EX_Type };
        Cursor c= ourdatabase.query(Food_TABLE, columns, EX_Date + " > " + dateOnly (fd) + " AND " + EX_Date + " < " + dateOnly(td), null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }
    

    If you don't use different timezones the dateOnly(java.util.Date d) can be optimized. Of course you can also use JODA-time.

    2. Save your Dates as TEXT

    If you choose this method your queries that are comparing the date-column are going to be a bit slower, but the entries in the database are human readable which doesn't have t be the case with method 1. TEXT-columns are ordered with BINARY by default, which means memcmp() is used to compare the values and to determine which value is greater or if the values are equal. (Remember x BETWEEN a AND b means x <= a AND x >= b.) You can examine the work of memcmp() with this function memcmp().

    To ensure you get the right results you have to ensure the following:

    1. All date-values in your database have to have the same text length.
    2. All date-values in your database have to be in the same Format. The bigger date-parts (year) have to be before the smaller date-parts (month).
    3. All Parameters for date-values in queries have to follow these rules too.

    A possible date-format may look like this: yyyy-MM-dd (for example 2014-02-04 or 2000-12-24).

    Advices

    • Use android.widget.DatePicker instead of Edittext for getting dates as input.
    • Never use any texts you got from user inputs directly in your query, but validate them before (see sql-injection).
    • Read some articles about how Strings are compared.
    0 讨论(0)
提交回复
热议问题