0

in my android project i am parsing a web page that includes list of items and record them to sqLite database. Because of the fact that page contents are changing, I need to update my database table on 2 months period. How can i do that ? Can i get the last inserted row date in SQLite ?

asyaben
  • 55
  • 2
  • 8

1 Answers1

0

You can insert the date-time when data is inserted into a column in your sqlite table.

Example:

Where you are calling your insert method you insert the time and date as a string like :

String getdateTime() {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(now);
    }

So your insert will look like

myDB.insert(getdateTime(),......);

Then inside your Sqlite handler class you can query for the last updated column which is your datetime.

 public String getLastDateTime() {
        String selectQuery = "SELECT * FROM " + MYTABLE + " ORDER BY datetimecolumn DESC LIMIT 1";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        String str = "0";
        if (cursor.moveToFirst())
            str = cursor.getString(cursor.getColumnIndex(KEY_COLUMN_DATETIME));
        cursor.close();

        return str;
    }

So getting that last column date time, compare it with the datetime now in the same format. Look at this for more on that.

If the time lapsed equals to 2 months run update.

Community
  • 1
  • 1
Steve Kamau
  • 2,755
  • 10
  • 42
  • 73