在Android SQLite中检索两个日期之间的数据

时间:2013-03-31 11:57:15

标签: android

我想在Android SQLite中的两个日期之间检索数据我在SQLiteAdapter类中使用了一个函数。我的功能是

public void history(String startdate,String enddate) {  
   Cursor mCursor = db.rawQuery("SELECT * FROM "+ KK_AIRLINEBOOK + 
                    " WHERE " + KEY_Bookingdate + 
                    " BETWEEN " + startdate + " AND " + enddate , null);        
}

但是它无法正常显示AND附近的语法错误

1 个答案:

答案 0 :(得分:13)

放置文字

public void history(String startdate,String enddate) {  
Cursor mCursor = db.rawQuery("SELECT * FROM "+ KK_AIRLINEBOOK + 
                " WHERE " + KEY_Bookingdate + 
                " BETWEEN '" + startdate + "' AND '" + enddate + "'", null);        
}  

但是如果你使用selectionArgs

会更好
public void history(String startdate,String enddate) {  
Cursor mCursor = db.rawQuery("SELECT * FROM "+ KK_AIRLINEBOOK + 
                " WHERE " + KEY_Bookingdate + 
                " BETWEEN ?  AND ?", new String[]{startdate, enddate});        
}