views:

337

answers:

4

Hi guys!

So, i have a application that displays tasks and i created an option in the Settings menu that lets you choose in how many days all (Never, 10 days, 20 days or 30 days) these tasks should be automatically deleted. So when i open up the app there will be a method that will be called and check if theres any tasks to be deleted. I did that to challenge myself, but i'm hitting the wall with my head and i cant come up with something. So here i am in need of help!

A little more information: i was trying to do something with java/android code, but from past searches i saw people dealing with it using SQL functions. Is it better?

Thanks in advance guys!

A: 

If I understand what you're asking...

I presume you'll need to add a date to the database entries, that plus obtaining a reliable source for the actual date that you're comparing those entries' dates to.

James
Sorry my english isnt that good! But yes, i already added dates each tasks in database but i dont know how to delete the tasks
Philipz
A: 

Please, i need your guys help!

Philipz
+1  A: 

Seems like a basic delete to me? See API here.


public int delete (String table, String whereClause, String[] whereArgs)

Parameters

table: the table to delete from

whereClause: the optional WHERE clause to apply when deleting. Passing null will delete all rows.


Returns the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass "1" as the whereClause.

Set your where clause to specify where CreatedDate (asusme you have this column - if not you will need something like it) < Now minus X days (where x is 10, 20 , 30 etc)

RM
alright. got it! does the <now-xDays statement works in method delete? or will i have to setup a execSQL?thanks RM!
Philipz
You should just be able to put it into the whereClause parameter, i.e. "CreatedDate < [YourDateHere]" i think...
RM
RM, thanks a lot for your answer! Im using execSQL :)
Philipz
A: 

Hey guys, i think im in the mid of the road, sorry for my ignorance but it just doesnt work.

Here's a piece of the code:

// opening the database, partial code
wDb=db.getWritableDatabase();

// the method to delete tasks with more than X days
public void deleteTasks(String daysTasks) {
wDb.execSQL("DELETE FROM " + TABLENAME + " WHERE " + COLUMNDATE + " < date('now', '-" + daysTasks + " days')");
}

Guess what? NullPointerException :(

Help me

Philipz
helppppppppp :(
Philipz
You need to publish more information than 'helpppppppppp' and NullPointer exception. Where is the null pointer exception. What is the schema for the table you are trying to delete from. Have you verified the command you are constructing is valid. Have you tried running the command on your desktop with another copy of SQLite database to test it. Unable to help until you have covered off these things for yourself.
RM
OK, thanks! Im gonna try it and then i post what i figured it out
Philipz
Hi! Done, i was doing some stupid stuff, but now everything is fine, thanks again RM!
Philipz