从Sqllite数据库检索数据两个日期之间的android [英] Retrieve Data from Sqllite DB between Two dates android

查看:124
本文介绍了从Sqllite数据库检索数据两个日期之间的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找回两个日期是一些如何得到正确的结果之间数据的一些如何输出,当我选择日期与月份它正常工作,但是当我选择超过一个月其输出空列表视图之间的日期下面是我的$ C空列表视图$ CS

下面我宣布在DB类变量

 公共静态最后弦乐EX_RowID =_id;
公共静态最后弦乐EX_Cattype =Ecattype;
公共静态最后弦乐EX_Date =Ecdate;
公共静态最后弦乐EX_Price =Ecprice;
公共静态最后弦乐EX_Type =ITYPE;
 

科瑞TABLE语句

 公共无效的onCreate(SQLiteDatabase DB){
        // TODO自动生成方法存根
        db.execSQL(CREATE TABLE+ Food_TABLE +(+
                EX_RowID +INTEGER PRIMARY KEY AUTOINCREMENT,+
                EX_Cattype +TEXT NOT NULL,+
                EX_Date +TEXT NOT NULL,+
                EX_Price +INTEGER NOT NULL,+
                EX_Type +TEXT NOT NULL UNIQUE);
                );

    }
 

输入数据的方法

 众长ExEntry(字符串Ecatgtype,EDATE字符串,字符串EPRICE,字符串Eitype){
// TODO自动生成方法存根

ContentValues​​ CV =新ContentValues​​();
cv.put(EX_Cattype,Ecatgtype);
cv.put(EX_Date,EDATE);
cv.put(EX_Price,EPRICE);
cv.put(EX_Type,Eitype);
返回ourdatabase.insertOrThrow(Food_TABLE,空,CV);
 }
 

在这里,我访问ExEntry方法

  ExMgDB Expentry =新ExMgDB(ADD_EX.this);
    Expentry.open();
    Expentry.ExEntry(美食,日期,价格,ITYPE);
    Expentry.close();
 

在这里,我这两个日期变量之间面临的问题

 公开光标CstmRpot(FD字符串,字符串TD){
// TODO自动生成方法存根
的String []列=新的String [] {EX_RowID,EX_Cattype,EX_Date,EX_Price,EX_Type};
光标C = ourdatabase.query(Food_TABLE,列,EX_Date +之间+ FD +'
 和+ TD +',NULL,NULL,NULL,NULL);
如果(C!= NULL){

       c.moveToFirst();

      }
返回℃;

}
 

我访问它像下面

  CustemRpt DBCS =新CustemRpt();
光标光标= CstDB.CstmRpot(frmdate,tondate);
 

解决方案

有两种主要的解决方案。所有解决方案的共同点,即包含日期列具有以某种方式排列。如果这个顺序被破坏你的数据被破坏和您的查询无法返回预期的结果!

1。保存日期为 INTEGER 在数据库中,并使用一种方法来映射日期到一个数字:

一个可能的方法是使用 Date.getTime()来映射日期为数字,但也有许多人。重要的是,

  1. 在相同日期得到相同数量和
  2. 这是另一个日期越来越更大的数字后的日期。

此方法排序将是正确的肯定。 为了实现这一目标用`Java.util.Date.getTime(),你只需要设定时间为0:00:00:如果你想只存储日期000

例如:

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

私有静态字符串dateOnly(java.util.Date D){
    日历CAL = Calendar.getInstance(); //特定于语言环境
    cal.setTime(四);
    cal.set(Calendar.HOUR_OF_DAY,0);
    cal.set(Calendar.MINUTE,0);
    cal.set(Calendar.SECOND,0);
    cal.set(Calendar.MILLISECOND,0);
    返回Long.toString(cal.getTimeInMillis());
}

公共光标CstmRpot(java.util.Date FD,java.util.Date TD){
    // TODO自动生成方法存根
    的String []列=新的String [] {EX_RowID,EX_Cattype,EX_Date,EX_Price,EX_Type};
    光标C = ourdatabase.query(Food_TABLE,列,EX_Date +>中+ dateOnly(FD)+和+ EX_Date +<+ dateOnly(TD),NULL,NULL,NULL,NULL);
    如果(C!= NULL){
        c.moveToFirst();
    }
    返回℃;
}
 

如果您不使用不同的时区的 dateOnly(java.util.Date D)可以进行优化。 当然,你也可以使用JODA时间。

2。保存日期为文本

如果您选择此方法查询所比较的日期栏将是一个有点慢,但在数据库中的条目是不为T与方法1的情况下,人类可读。 文本 -columns排序, BINARY 默认情况下,这意味着 memcmp()用来比较的值,并确定其值大于或如果值相等。 (还记得 X之间的和b 办法 X< = A和X> = B ) 您可以检查的工作memcmp()具有这种功能的 memcmp()

要确保你得到你要保证以下正确的结果:

  1. 在数据库中的所有日期值必须具有相同的文本长度。
  2. 在数据库中的所有日期值必须在相同的格式。更大的日期部分(年)必须是较小的日期部分(月)之前。
  3. 在查询所有参数的日期值必须遵循这些规则了。

一个可能的日期格式可能是这样的: YYYY-MM-DD (例如 2014年2月4日 2000年12月24日)。

通知书

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 dates between more than one month it output empty listview below is my codes

here i declare the variable in DB class

public static final String EX_RowID = "_id";
public static final String EX_Cattype = "Ecattype";
public static final String EX_Date = "Ecdate";
public static final String EX_Price = "Ecprice";
public static final String EX_Type = "itype";

creat table statement

    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + Food_TABLE +"(" +
                EX_RowID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                EX_Cattype + " TEXT NOT NULL, " +
                EX_Date + " TEXT NOT NULL," +
                EX_Price + " INTEGER NOT NULL," +
                EX_Type + " TEXT NOT NULL UNIQUE );"
                );

    }

enter data Method

 public long ExEntry(String Ecatgtype, String Edate, String Eprice, String Eitype) {
// TODO Auto-generated method stub

ContentValues cv = new ContentValues();
cv.put(EX_Cattype, Ecatgtype);
cv.put(EX_Date, Edate );
cv.put(EX_Price, Eprice);
cv.put(EX_Type, Eitype);
return ourdatabase.insertOrThrow(Food_TABLE, null, cv);
 }

here i access the ExEntry Method

ExMgDB Expentry = new ExMgDB(ADD_EX.this);
    Expentry.open();
    Expentry.ExEntry(cate, date, price, itype);
    Expentry.close();

here i am facing the problem between these two dates variables

 public Cursor CstmRpot(String fd, String 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  + " BETWEEN '" + fd + "'  
 AND '" + td + "'" , null, null, null, null);
if (c != null) {

       c.moveToFirst();

      }
return c;

}

i access it like below

 CustemRpt dbcs = new CustemRpt();    
Cursor cursor = CstDB.CstmRpot(frmdate,tondate);

解决方案

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.

这篇关于从Sqllite数据库检索数据两个日期之间的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆