I am trying to make an android application that will identify how much time is left for a task to be completed. I have followed Vogella's tutorial, particularly this part http://www.vogella.com/articles/AndroidSQLite/article.html#todo to make a contentprovider and database. It populates a listview with two things, the name of the task and how many days are left (the latter is calculated when the user selects his end date in another activity). My app calcualtes the current date and subtracts it from the end date and stores how many days are left in the database. The problem is that this is only stored once. Three days from now it will still say 4 days left. I want the app to check for how many days are left every time the client starts it (check current date, subtract from end date and update that column in the database). The problem is I'm not sure how to do it. If somebody could give me some guidance I would appreciate it.
do the calculation then do getContentResolver().update(uri,values,selection,selectionArgs);
EDIT:
so just update with the values
ContentValues values = new ContentValues();
values.put(HabitTable.TIME); //whatever column you want to update, I dont know the name of it
...//any other values you want to update too
getContentResolver().update(HabitTable.CONTENT_URI,values,HabitTable.ID+"=?",new String[] {String.valueOf(id)}); //id is the id of the row you wan to update
obviously you will need to replace stuff with the correct column names
Related
QUESTION: Im making a flashcard app and when the user clicks "easy" for example, I want to increase the time of the card, then that card won't appear until the time goes reaches 0.
e.g. "Card 1" has a time of 0 at default , user clicks "I know button" and the time on that card increases to 5 mins (5:00), then that card wont appear again for 5 minutes until timer is back to 0, is this possible to do?
is this possible to do?
I believe that if you consider what SQLite is capable of and what an Android App is capable of then Yes. However using SQLite alone then No.
Typically, to get your outcome, the time would remain constant in the database but you would extract and thus show only the rows that met the criteria e.g. the time stored is less than or equal to the current time.
Clicking I know would then update the respective row and set the value to the current time plus 5 minutes, thus an extract, which could be timer based would then not show the respective row as it's then greater than the current time.
As for the timer SQLite does not have a built in timer. It is a database manager whose job is to store and retrieve structured data.
As an example consider the following, which shows the principle:-
DROP TABLE IF EXISTS flashcard;
CREATE TABLE IF NOT EXISTS flashcard (cardtitle TEXT, cardtimer INTEGER);
INSERT INTO flashcard VALUES ('Card1',strftime('%s','now')),('Card2',strftime('%s','now','+1 minute')),
('Card3',strftime('%s','now','+2 minute'));
SELECT *,strftime('%s','now') FROM flashcard WHERE cardtimer <= strftime('%s','now');
UPDATE flashcard SET cardtimer = strftime('%s','now','+5 minutes') WHERE cardtitle = 'Card1';
SELECT *,strftime('%s','now') FROM flashcard WHERE cardtimer <= strftime('%s','now');
This:-
drops and creates a table with 2 columns,
a column named cardtitle that stores the title of the card
a column named cardtimer for the time when the card can be displayed on or after
adds 3 rows with unique titles the time fors the first being the current time, the time for the second 1 minute later,and for the third another minute later.
An extract (SELECT query) that displays only the rows where the timer is now or earlier (irrespective of when it is run).
this shows just the first of the 3 rows as the others are in the future
An Update that changes the time of the first row to be 5 minutes from now
Another Extract using the same query as per 3
this shows nothing because now all 3 rows are in the future
Running the above results in :-
i.e. just Card1 is extracted
Then :-
i.e. all of the cards are now in the future.
However if the same extract is then run a while later (but sooner then 5 minutes later), as can be seen some 139 seconds later, then :-
If then after 5 minutes and the same extract is run then :-
I have a hybrid Cordova Android mobile app which records user activity in a SQLite database. Each row entry has columns that help characterize the nature of the activity. One such column records three sub-activities on different days of the week which I represent as an integer array [0..6,7..13,14..20] for the three sub-activities. Further I want to be able to distinguish between activities on "normal" days and days that happen to be public holidays. I do so by recording a negative entry in the array. To make this clearer here is an example
[0,-2,9,13,-18,20]
is representing
Sub activity A on Sunday
Sub activity A on public holiday Tuesday
Sub activity B on Wednesday & Saturday
Sub activity C on public holiday Friday
Sub-activity C on Saturday
This is relatively straightforward - I can store the array as JSON. However, I also want to be able to query this set for membership as efficiently as possible. The only way I can think of is using a LIKE condition. e.g.
SELECT * FROM TABLE WHERE activity_column LIKE '%-2%'
which I would expect to return all rows where there is recorded activity on public holiday Tuesdays.
I suspect this will probably work. However, I am a newbie when it comes to Android datbases. I know that SQLite is the default solution. Is there an option - either in SQLite or in an alternative - which can render this kind of storage & search more efficient and less convoluted?
This all goes down to how you design your sql base.
One approach would probably be to have 3 tables, one for each activity. The key in each could be the timestamp of the day, and then you could have columns for the json array of your int data.
You can also add an additional quality-of-life column such as "day" where you could keep the int representation of the day of the week.
This way if you wanted data for Activity1 registered on Tuesdays, you would just query
SELECT * FROM Activity1 WHERE day = 1
I am relatively new to Android and SQLite, I was wondering I can create basic form input where user can input his data to be added to SQLite table
When it comes to selecting the day, Monday, tuesday...etc is it possible to compare current day that was added in SQLite to real time day so that for example if its monday, it will retrieve all data with "Monday" as their Day column.
If I want to retrieve all reminders is it possible to retrieve all reminders throughout the week and place them not in an exapandable view but in the associated header day so for example in the same list each day will have a header "Monday" and all monday reminders will be placed in the monday header, will this mean I need multiple ListViews, I will be implementing this on a Fragment
I want to know what you actually want to ask?
1. you can put users' input data into sqlite database
2. put into the db table you created with values(input data & current date)
3. get data from the database when needed, and calculate the date difference and create a listview by asynctask.
i have no reputation to write a comment but want to give u some help. hope you add some comment about what you want to know about specifically in detail.
Well as title says, I'm trying to set a column value based on the sum of two columns in the same table, I mean, I have a row on a table where I have some attributes "capital,income,mat_expense,other_expense,net_profit" I will be updating this row everytime I sell a new product or register some payments, when I sell a new product, I will update the "income" attribute adding the sale price, when I register a mat_expense(raw material expense) I will update that attribut adding the new expense price the same action with other_expense, my point is I want to calculate the net_profit of my sell, If i sell 20$ and I spend 10$ on raw material, I want my net_profit attribute to be 10, and make the same operation everytime I update de the table, the same thing I wanna do with the "capital" attribute (income - (mat_expense + other_expense), that is basiclly what I need to do, I've been reading this operation should be done by a "trigger" on sqlite, I have been reading some post but I don't get how to fit it to my case, can you guys give me a hands with this? Example:
| capital | income | mat_expense | other_expense | net_profit |
5 20 10 5 10
By the way, this is a consult, could be possible to make a trigger which make an attribute acts as a accumulator? as I explained before, I'll be upgrading some attributes, adding new values,everytime I do that, I need to consult the currently value save it in a Variable then I sum the new value, which I think It's not much efficient.
Thank you so much by reading and I really thank any help you guys can give me.
In SQL you can use expressions to define new columns, ie:
select
income,
mat_expense,
other_expense,
income - (mat_expense + other_expense) as capital
from your_table;
You will get the 4rd column called 'capital'
As for the second question - you should use such calculated virtual columns whenever it's possible. Expressions may be quite complex and include SQL functions, and even include subqueries. For example you can add a column with minimal value from other table rows correlated with current row from the first table.
Generally SQL language is about transforming source sets of data into other representations, some tables into others.
When you can't calculate result set in single SQL statement, then you may have to calculate intermediate temporary data/variables, may be via temp tables/cursors etc, but it is the last thing one should do. Still we can't avoid it sometimes
What is the best way to maintain a "cumulative sum" of a particular data column in SQLite? I have found several examples online, but I am not 100% certain how I might integrate these approaches into my ContentProvider.
In previous applications, I have tried to maintain cumulative data myself, updating the data each time I insert new data into the table. For example, in the sample code below, every time I would add a new record with a value score, I would then manually update the value of cumulative_score based on its value in the previous row.
_id score cumulative_score
1 100 100
2 50 150
3 25 175
4 25 200
5 10 210
However, this is far from ideal and becomes very messy when handling tables with many columns. Is there a way to somehow automate the process of updating cumulative data each time I insert/update records in my table? How might I integrate this into my ContentProvider implementation?
I know there must be a way to do this... I just don't know how. Thanks!
Probably the easiest way is with a SQLite trigger. That is the closest I know
of to "automation". Just have an insert trigger that takes the previous
cumulative sum, adds the current score and stores it in the new row's cumulative
sum. Something like this (assuming _id is the column you are ordering on):
CREATE TRIGGER calc_cumulative_score AFTER INSERT ON tablename FOR EACH ROW
BEGIN
UPDATE tablename SET cumulative_score =
(SELECT cumulative_score
FROM tablename
WHERE _id = (SELECT MAX(_id) FROM tablename))
+ new.score
WHERE _id = new._id;
END
Making sure that the trigger and the original insert are in the same
transaction. For arbitrary updates of the score column, you would have to
have to implement a recursive trigger that somehow finds the next highest id (maybe by selecting by the min id
in the set of rows with an id greater than the current one) and updates its
cumulative sum.
If you are opposed to using triggers, you can do more or less the same thing in
the ContentProvider in the insert and update methods manually, though since
you're pretty much locked into SQLite on Android, I don't see much reason not to
use triggers.
I assume you are wanting to do this as an optimization, as otherwise you could just calculate the sum on demand (O(n) vs O(1), so you'd have to consider how big n might get, and how often you need the sums).