0

我已将日期作为字符串存储在 sqlite 数据库表中。我需要什么 ii 必须检索过去 7 天内存储的值。我已经尝试使用以下但它没有显示任何值。

我的数据库功能

     public Cursor paymentWeek(Activity activity)
     {

       String[] from = { _PAYMENTID, NAME, REQUESTEDDATE, FROMAD, TOADD, EMAILBODYPAYMENT, AMOUNT};  

       SQLiteDatabase db = getReadableDatabase(); 

       String orderby = REQUESTEDDATE+" DESC";  

       Cursor cursor = db.rawQuery("select * from " + PAYMENTTABLE + " where " + REQUESTEDDATE + " BETWEEN "
       + "date('now')" + " AND " + "date('now','-7 days')", null);

       activity.startManagingCursor(cursor);

       return cursor;  
    }

Calling function

    Cursor week = db.paymentWeek(this);
               String[] weekly = new String[] { PaymentAppDataBase.REQUESTEDDATE,PaymentAppDataBase.PAYMENTNAME,PaymentAppDataBase.AMOUNT };
               int[] sevendays = new int[] { R.id.Date,R.id.Name,R.id.Amount };

               SimpleCursorAdapter weekcursor =
                    new SimpleCursorAdapter(this, R.layout.listview, week, weekly, sevendays);
                setListAdapter(weekcursor);
               db.close();

如果你们解决这个问题会很有帮助。我被困在这里。

4

2 回答 2

1

try Following query to get records for last week(7 days):

"select * from " + PAYMENTTABLE + " where " + REQUESTEDDATE + "DATE_SUB( CURDATE( ) ,INTERVAL 7 DAY ) AND CURDATE( )";
于 2012-04-09T04:35:01.207 回答
0

您不必更改数据库设计。

只需编写您自己的从 dateString 转换为 long 方法,并将其放在 DateUtil.java 之类的类中,然后在您想要对日期类型执行某些操作的任何地方调用它。

该方法应如下所示:

    public static long convertStringDateToLong(String date, String yourDateFormat) {
    Long time = 0L;
    try {
        SimpleDateFormat df = new SimpleDateFormat(yourDateFormat);
        Date d = null;
        try {
            d = df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        time = d.getTime();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return time;
}

你的日期格式应该是这样的:“MM/dd/yyyy”

于 2013-12-10T07:05:50.463 回答