1

I'm very new to Java so please forgive any mistakes I make.

I'm currently developing an automated TODO list application that sorts through a list of tasks inputted by the user and prioritizes the more important ones.

After the user has added the task, it will be added into an SQLite database. I'd like to know how I could associate an Integer with each individual task.

For example: Say there's an Integer called Priority which has a default value of 10. With each task that's added they begin with a default value of Priority = 10 but change according to user demand.

So if one task's default value is Priority = 10, Priority could increase 11, whereas another task on the same list that starts at Priority = 10 may be reduced to Priority = 9. I'm very unsure as to how I can make every task added to the database have an individual value which changes regularly.

If you would like me to expand on this further I would be happy to as my explanation may be confusing.

Thanks in advance!

0

1 Answer 1

1

You could perhaps use a TRIGGER SQL As Understood By SQLite - CREATE TRIGGER

For example, consider the following :-

DROP TABLE IF EXISTS tasks;
CREATE TABLE IF NOT EXISTS tasks (ID INTEGER PRIMARY KEY, name TEXT, user_reference INTEGER, priority DEFAULT 10);
DROP TRIGGER IF EXISTS set_task_priority;

-- Create the Trigger
CREATE TRIGGER IF NOT EXISTS set_task_priority 
    AFTER INSERT ON tasks
    WHEN ((SELECT count() FROM tasks WHERE user_reference = new.user_reference) > 3)
    BEGIN   
        UPDATE tasks SET priority = new.priority + (SELECT  count() FROM  tasks WHERE user_reference = new.user_reference)
        WHERE ID = new.ID
     ;
    END;

-- Add some data to test the trigger
INSERT INTO tasks (user_reference,name) VALUES 
    (1,'CLEAN'),(1,'TIDY'),(1,'VACUMN'),(1,'WASH'),(1,'THINK'),
    (2,'CLEAN'),(2,'TIDY'),(2,'VACUMN'),(2,'WASH'),
    (3,'CLEAN'),(3,'TIDY'),(3,'VACUMN'),(3,'WASH'),(3,'THINK'),(3,'STUDY'),
    (3,'CLEAN'),(3,'TIDY'),(3,'VACUMN'),(3,'WASH'),(3,'THINK'),(3,'STUDY'),
    (3,'CLEAN'),(3,'TIDY'),(3,'VACUMN'),(3,'WASH'),(3,'THINK'),(3,'STUDY'),
    (3,'CLEAN'),(3,'TIDY'),(3,'VACUMN'),(3,'WASH'),(3,'THINK'),(3,'STUDY')
;
INSERT INTO tasks (user_reference,name) VALUES (3,'SLEEP');
SELECT * FROM tasks;
  • Note, this is not intended to be a solution to your issue (that as you say isn't explained), but an example of how a TRIGGER can automatically apply changes (in this case after a row has been inserted).

The following results in :-

enter image description here

You say

I'd like to know how I could associate an Integer with each individual task.

Each task in the above has it's own unique integer, the rowid id or in the case above id as an alias of the rowid. Rowid Tables.

Perhaps even something like :-

CREATE TABLE IF NOT EXISTS tasks (ID INTEGER PRIMARY KEY, name TEXT, user_reference INTEGER, priority DEFAULT (ABS(random() % 100)));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much this is exactly what I was looking for! I really appreciate the help here!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.