具有时间戳的ContentResolver查询

时间:2017-09-12 19:26:04

标签: android android-sqlite android-contentresolver

如果当前日期和交易日期之间的差异少于15分钟,我必须运行服务。

我有DBHelper类创建表:

public class ToDoDBHelper extends SQLiteOpenHelper {

private final int DATABASE_VERSION = 1;
private ArrayList<ToDoClass> todoitems;

private final String DATABASE_NAME = "deals.db";

public ToDoDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}


@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

    final  String Create_Table_Query = "CREATE TABLE "+ ToDoDatabase.ToDoDatabaseEntry.TABLE_NAME+" ( "+
    ToDoDatabase.ToDoDatabaseEntry._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
            ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column+" TIMESTAMP ,"+
            ToDoDatabase.ToDoDatabaseEntry.end_dealDate_column+" TIMESTAMP ,"+
            ToDoDatabase.ToDoDatabaseEntry.shortdescription_column+" TEXT  NOT NULL ,"+
            ToDoDatabase.ToDoDatabaseEntry.fulldescription_column+" TEXT NOT NULL "+");";
    sqLiteDatabase.execSQL(Create_Table_Query);

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ToDoDatabase.ToDoDatabaseEntry.TABLE_NAME);
    onCreate(sqLiteDatabase);

}

}

我有内容解析器查询,我想要使用选择标准:

 Cursor cursor = context.getContentResolver().query(
            ToDoDatabase.ToDoDatabaseEntry.CONTENT_URI,
            null,
            selection,
            null,
            ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column+" DESC");

但我不知道如何写选择。在选择中,我需要区分ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column和currentDate。

请帮忙

2 个答案:

答案 0 :(得分:1)

@Shriyansh Gautam感谢您的回答。 我找到了问题的解决方案。现在我使用的是SQLiteDatabase类的查询方法。这是我的代码,它对我有用。

   Date curDate = new Date();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(curDate);
    calendar.set(Calendar.MINUTE, (calendar.get(Calendar.MINUTE)+15));


    String selection = ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column+" >= ?";
    String [] selectionArgs = {String.valueOf(calendar.getTimeInMillis())};

    String notifText = null;
    ToDoDBHelper toDoDBHelper  = new ToDoDBHelper(context,"deals", null, 1);
    SQLiteDatabase sqLiteDatabase = toDoDBHelper.getWritableDatabase();

    Cursor cursor = sqLiteDatabase.query("deals",
            null,
            selection,
            selectionArgs,
            null,
            null,
            ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column+" ASC");

答案 1 :(得分:0)

你可以试试这个

       long FIFTEEN_MINUTES = 15*60*1000;
       String dateFormatSQLite = "yyyy-MM-dd HH:mm:ss"; //verify timestamp format in your case
       String timezone = "GMT+0.00"; //use your timestamp
       SimpleDateFormat sdf = new SimpleDateFormat(dateFormatSQLite);
       sdf.setTimeZone(TimeZone.getTimeZone(timezone));
       String date = sdf.format(new Date(System.currentTimeMillis() - FIFTEEN_MINUTES));

如果在您的情况下,该值存储在sqlite数据库的epoch中,那么您可以执行此操作

       long FIFTEEN_MINUTES = 15*60*1000;
       String date = System.currentTimeMillis() - FIFTEEN_MINUTES;


      Cursor cursor = context.getContentResolver().query(
        ToDoDatabase.ToDoDatabaseEntry.CONTENT_URI,
        null,
        ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column+" <= ?",
        new String[]{date},
        ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column+" DESC");