如何将日期作为参数发送以从数据库中获取记录

时间:2014-02-26 06:45:35

标签: android

我想从过去一周的数据库中获取记录。我在7天前获取当前日期和日期,然后将它们发送到数据库处理程序,但它返回错误:

The method `getALLCompJobs(String, String)` in the type `DatabaseHandler` is not applicable for 
the `arguments (Date, Date)`

以下是我的代码。请帮我弄清楚如何从数据库中获取过去一周的记录。

           public class JobSchema extends BackBaseActivity {

            Date dateBefore7day;
        Date CurrentDate;


         @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jobsschema);
             Calendar c = Calendar.getInstance();

             SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy   HH:mm");
        df.format(c.getTime());
       CurrentDate=c.getTime();
               c.add(Calendar.DAY_OF_MONTH, -7);

              dateBefore7day=  c.getTime();



                    DatabaseHandler db = new DatabaseHandler(JobSchema.this);
            jobarr = db.getALLCompJobs(CurrentDate,dateBefore7day);


         <---error show  The method getALLCompJobs(String, String) in the type 
       DatabaseHandler is not applicable for the arguments (Date, Date) --?


        public class DatabaseHandler extends SQLiteOpenHelper {

              String CREATE_COMPLETED_TABLE = "CREATE TABLE " + TABLE_COMPLETED_JOBS
            + "(" + KEY_COMPID + " INTEGER PRIMARY KEY," + KEY_TIMEJOB
            + " TEXT," + KEY_TIMEWEEK+ " DATE,"    + KEY_PICK + " 
                          TEXT," + KEY_DEST + " TEXT,"
            + KEY_FARE + " TEXT" + ")";


                  public List<JobSchmeModel> getALLCompJobs(String CurrentDate, String 
          dateBefore7day) {
//public List<JobSchmeModel> getALLCompJobs() {
    List<JobSchmeModel> compjobsList = new ArrayList<JobSchmeModel>();

        String selectQuery = "SELECT  * FROM " + TABLE_COMPLETED_JOBS + " WHERE "  
      + KEY_TIMEWEEK + ">=" + dateBefore7day + " AND " + KEY_TIMEJOB + "<=" + 
       CurrentDate + " ORDER BY "+KEY_COMPID+" DESC";
            SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

2 个答案:

答案 0 :(得分:0)

如果您的语言支持,您可以使用一种名为&#34; epochtime&#34; (1970年1月1日以来的秒数)。在此表单中添加或减去日期的简单算术非常有效。

答案 1 :(得分:0)

好吧,请阅读错误:它告诉您将错误类型的参数传递给getALLCompJobs方法。它需要String个参数,您传递的参数类型为Calendar。试试这个

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy   HH:mm");
String sCurrent = df.format(c.getTime());
c.add(Calendar.DAY_OF_MONTH, -7);
String sWeekBefore = df.format(c.getTime());
jobarr = db.getALLCompJobs(sCurrent, sWeekBefore);

UPD 日期字符串的格式看起来不正确。

检查this answer,了解如何将java.util.Date转换为java.sql.Date,并且不要忘记在传递给.toString()的日期对象上调用db.getALLCompJobs(...)