2012-08-13 2 views
3

我是新的sqlite。我有一個表,有3列android-sqlite日期查詢

1. Id 
2. FoodName 
3. Date and time 

(ex: '1' 'Chicken' '2012-08-10 13:54') 

我讀的地方Android sqlite不提供日期數據類型。

所以我使用文本數據類型在本地數據庫中存儲日期。它工作正常。

現在我想編寫一個查詢來檢索兩個日期之間的記錄。我嘗試過,但我沒有得到它。

== >>代碼

public class FoodDB { 

//Database Related Constants 
public static final String KEY_ROWID = "id"; 
public static final String KEY_NAME = "fooditem"; 
public static final String KEY_TIMESTAMP = "timestamp"; 

private static final String DATABASE_NAME = "AJFoodDB"; 
private static final String DATABASE_TABLE = "AJ_Food"; 
private static final int DATABASE_VERSION = 1; 

Cursor c; 
long id; 

private static final String DATABASE_CREATE = 
    "create table "+DATABASE_TABLE+" (id integer primary key autoincrement, "    
    + KEY_NAME+" text not null," 
    + KEY_TIMESTAMP+" text not null);"; 

private final Context context; 
private DatabaseHelper DBHelper; 
private SQLiteDatabase sqlitedatabase; 

public FoodDB(Context ctext){ 
    context = ctext;   
} 

private static class DatabaseHelper extends SQLiteOpenHelper 
{ 
    DatabaseHelper(Context context) 
    { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    public void onCreate(SQLiteDatabase db) 
    { 
     db.execSQL(DATABASE_CREATE);      
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
     db.execSQL("DROP TABLE IF EXISTS titles"); 
     onCreate(db); 
    } 
} 

//Insert Row 
public void insertSSFiltersDetails(String keyname, String timestamp){ 
    try 
    { 
     DBHelper = new DatabaseHelper(context); 
     sqlitedatabase = DBHelper.getWritableDatabase(); 
     insertRow(keyname, timestamp);  
     sqlitedatabase.close(); 

    } catch (Exception e) { 
     System.out.println("Error from inserting data->"+e.toString()); 
    } 
} 

public long insertRow(String keyname, String timestamp){ 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_NAME, keyname); 
    initialValues.put(KEY_TIMESTAMP, timestamp);  
    return sqlitedatabase.insert(DATABASE_TABLE, null, initialValues); 
} 

//Close Data Base 
public void closeDBHelper(){ 
    if(DBHelper != null){ 
     DBHelper.close(); 
    }  
    if (c != null) { 
     c.close(); 
     c = null; 
    } 
}  } 

查詢和表 SELECT * FROM AJ_Food;

1|rice |8/12/2011 16:11 
2|Rice |8/13/2012 11:42 
3|chicken|8/13/2012 11:42 
4|Prawans|8/13/2012 11:43 

回答

4

即使您在sqlite中爲日期拍攝了文字。您可以編寫以下查詢以在兩個日期之間選擇數據。

select column from Table where columnDate between '2012-07-01' and '2012-07-07' 
+0

源碼>從AJ_Food其中 '2012-07-01' 之間時間戳和 ' 2012-07-07' 選擇時間戳; 從AJ_Food選擇時間戳,其中'2012-07-01'和'2012-07- 07'之間的時間戳; – naresh 2012-08-13 06:52:31

+0

@naresh這條信息是否對您有幫助? – 2012-08-13 08:19:17

+0

是的它正在工作 – naresh 2012-08-13 09:15:43

0
select col from Table where Date < '2011-08-02' and Date >'2012-08-13' 

或以其他方式

select col from Table where Date between '2011-08-02' and '2012-08-13' 
+0

sqlite>從AJ_Food選擇時間戳日期<'2011-08-02'和日期''2012- 08-13'; 從AJ_Food選擇時間戳,其中日期<'2011-08-02'和日期>'2012-08-13'; SQL錯誤:沒有這樣的列:日期 – naresh 2012-08-13 06:50:23

1

可以使用date()函數轉換stringdate

SELECT * 
FROM AJ_Food 
WHERE date(dateTimeColumn) BETWEEN date('startDateHere') AND date('endDateHere') 

See more Formats HERE

+0

在表中存儲日期格式是正確的方式? – naresh 2012-08-13 06:53:34