4

我是新的sqlite。我有一张有 3 列的表

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

3 回答 3

5

即使您在 sqlite 中使用文本作为日期。您可以编写以下查询来选择两个日期之间的数据。

select column from Table where columnDate between '2012-07-01' and '2012-07-07'
于 2012-08-13T06:22:23.533 回答
1

您可以转换stringdate使用date( )功能。

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

在此处查看更多格式

于 2012-08-13T06:29:34.457 回答
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'
于 2012-08-13T06:29:15.113 回答