• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Insert and query date in sqlitedatabase

coder05

Lurker
Feb 21, 2014
5
0
Hi,
I am inserting date in the database.Now I want to query the date for the last 7 days and get all the names which have been added in the last 7 days. How can I achieve that?


[HIGH]public static final String KEY_ROWID = "_id";
public static final String KEY_NAME ="persons_name";
public static final String KEY_DATE = "current_date";

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_DATE + " TEXT NOT NULL, " +
KEY_WANDERING + " TEXT NOT NULL);"

);
}
[/HIGH]
//to insert data in database
[HIGH]ContentValues cv = new ContentValues();
cv.put(KEY_DATE, System.currentTimeMillis());
cv.put(KEY_NAME , name);
[/HIGH]
//I am trying to query date for the last 7 days
[HIGH]String[] columns = new String[]{KEY_ROWID, KEY_NAME};
String result =" ";

long sevenDays = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7L;
Cursor c = ourDatabase.query(DATABASE_TABLE, null, KEY_DATE + " > "+ sevenDays, null, null, null, null);
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);

for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){

result = result + c.getString(iName) + ",";
}

return result;
[/HIGH]
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones