如何匹配或存储在sqlite的android系统中的日期比较日期字符串? [英] How to match or compare a date string with the date stored in sqlite in android?

查看:116
本文介绍了如何匹配或存储在sqlite的android系统中的日期比较日期字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我工作的这最后3或4周,但有没有能够解决这个问题。
我工作在Android的SQLite数据库中,我在活动中使用日期选取器控件一旦用户设置一个日期,然后我会保存日期为字符串,然后将其发送到数据库它工作正常,但是当我试图检索日期日期选取器控件中设置的基础上的日期则它不给我所需的输出显示它只有一条线是在我打开目录下载数据库。

数据库类

  //表使用列定义
私有静态最后弦乐CREATE_MYCOMPANY =
    创建表公司+(+_id+整数主键自动增量
    +company_date+号);
//插入方法
众长创建(字符串str){    ContentValues​​ ARGS =新ContentValues​​();
    args.put(KEY_COMPANY_DATE,STR);    返回mDb.insert(myCompany中,空,参数);
}
//方法对于撷取Date.//的基础上的行
    公共光标fetchdate(字符串datr)抛出的SQLException,ParseException的{    光标mCursor =
        mDb.query(真,myCompany中,新的String [] {KEY_COMPANY_ID,
                KEY_COMPANY_DATE},KEY_COMPANY_DATE / * +之间date('2012-6-10')和日期('2012-6-14')* /
                 +=+ datr,空,
                NULL,NULL,NULL,NULL);    如果(mCursor!= NULL){
        mCursor.moveToFirst();
    }
    返回mCursor;}

活动课code

  //日期选择器
DatePicker的DT =(的DatePicker)findViewById(R.id.datePicker1);//转换日期为字符串格式
串天=将String.valueOf(dt.getYear())+ - +将String.valueOf(dt.getMonth()+1)
            + - +将String.valueOf(dt.getDayOfMonth());
//对于日期的基础上显示表中的行的方法
公共无效DisplayAllCmpData(){        employeeTable.open();
        尝试{
          光标C = employeeTable.fetchdate(2012-6-13);
          如果(c.moveToFirst())
           {
             做{
              的System.out.println(BOOL2);
                 DisplayCmpTitle(C);
                }而(c.moveToNext());
           }
         }赶上(例外五){
        的System.out.println(E);
     Toast.makeText(这一点,e.toString(),Toast.LENGTH_LONG).show();
           }
         employeeTable.close();
    }//功能用于显示所有record.//
    公共无效DisplayCmpTitle(光标C)
    {    的System.out.println(布尔);
       Toast.makeText(这一点,
           ID:+ c.getString(0)+\\ N+
           日期:+ c.getString(1)
           ,Toast.LENGTH_LONG).show();    }

感谢提前任何帮助!!!!!!


解决方案

  mDb.query(真实的,myCompany中,新的String [] {KEY_COMPANY_ID,
                KEY_COMPANY_DATE},KEY_COMPANY_DATE / * +之间date('2012-6-10')和日期('2012-6-14')* /
                 +='+ datr +',空,
                NULL,NULL,NULL,NULL);

Actually I am working on this for the last 3 or 4 weeks but have not able to resolve it. I am working on Sqlite Database in android in which I have used a date picker control in my activity Once a user sets a date then i'll stored the date into the string and then sends it to the database it is working fine but when i tried to retrieve the date on the basis of date set on date picker control then it doesn't give me the desired output it show the only a line which is opening a database on my catlog.

Database Class

//Table Definition with Columns
private static final String CREATE_MYCOMPANY =
    "create table company " + " (" + "_id" + " integer primary key autoincrement, "
    + "company_date" +" date);";


//Insert Method
public long create(String str) {

    ContentValues args = new ContentValues();
    args.put(KEY_COMPANY_DATE, str);

    return mDb.insert(MYCOMPANY, null,args);
}




//Method For Fetching the row on the basis of Date.//
    public Cursor fetchdate(String datr) throws SQLException, ParseException {

    Cursor mCursor =
        mDb.query(true, MYCOMPANY, new String[] {KEY_COMPANY_ID,
                KEY_COMPANY_DATE}, KEY_COMPANY_DATE /*+" BETWEEN date('2012-6-10') AND date('2012-6-14')"*/ 
                 + " = " + datr, null,
                null, null, null, null);

    if (mCursor != null) {
        mCursor.moveToFirst();
    }


    return mCursor;

}

Activity Class Code

//Date picker 
DatePicker  dt = (DatePicker)findViewById(R.id.datePicker1);

//Converting the Date into the string format  
String day = String.valueOf(dt.getYear())+"-" +String.valueOf(dt.getMonth() + 1)
            +"-"+String.valueOf(dt.getDayOfMonth());


//Method for displaying the rows of table on date basis
public void DisplayAllCmpData(){

        employeeTable.open();
        try{
          Cursor c = employeeTable.fetchdate("2012-6-13");
          if (c.moveToFirst())
           {
             do {
              System.out.println("bool2");
                 DisplayCmpTitle(c);
                } while (c.moveToNext());
           }
         }catch(Exception e){
        System.out.println(e);
     Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
           }
         employeeTable.close();
    }

//Function For Displaying the all record.//    
    public void DisplayCmpTitle(Cursor c)
    { 

    System.out.println("bool");
       Toast.makeText(this,
           "ID: " + c.getString(0) + "\n" +
           "DATE: " + c.getString(1)
           , Toast.LENGTH_LONG).show();

    }

Thanks for any help in advance!!!!!!

解决方案

mDb.query(true, MYCOMPANY, new String[] {KEY_COMPANY_ID,
                KEY_COMPANY_DATE}, KEY_COMPANY_DATE /*+" BETWEEN date('2012-6-10') AND date('2012-6-14')"*/ 
                 + " = '" + datr+"'", null,
                null, null, null, null);

这篇关于如何匹配或存储在sqlite的android系统中的日期比较日期字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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