GVillani82
GVillani82

Reputation: 17429

Performing a delete statement with datetime in the where clause

In my android application I perform some insert in a table which has a datetime column. For creating the data to inserd in that column I use the following code:

Date d = new Date((new Date()).getTime()+60000);
String exDate= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.ITALY).format(d);

and I have added this String in a DATETIME column (named timestamp) of my table.

Now I want perform a query in that table, which deletes all the rows in which the date in the timestamp column is greater then a givend Date. Something like this:

DELETE FROM mytable WHERE timestamp > mydate

I was trying to use the delete(String table, String whereClause, String[] whereArgs) method inside the SQLiteDatabase library.

Someone can help me?

Upvotes: 0

Views: 279

Answers (1)

Budius
Budius

Reputation: 39846

in my experience, using a String as a date-time field is a bad choice. Use a long instead. Android can create/set Date or Calendar objects based on the Unix Epoch and comparing a long in the sqlLite is a breeze.

So yeah, my answer is: change the SQL create statement to use a INTEGER instead of TEXT and you can easilt call the delete statement you're trying to do.

Upvotes: 1

Related Questions