Efficiently storing & querying integer arrays in SQLite - android

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

Related

Best practice to store days in sqlite

Hello I'm creating an android app.
I would like to know what would be the best practise to save and request a event on a special day.
I have a list of events that should repeat at different days.
For example I would like to have an event on Monday and Friday.
This event should only be shown on this days.
I found some solutions to store the data but I dont really know how to build the query for sqlite to request just selected day.
To store data I found this Storing DaysOfWeek as single integer in sqlite database using enum/bitwise - Java
But I dont know how to build the query for example just for monday in sqlite.
Or is there a better workaround?
Best regards
This is just an idea And I even don't know it will work or not. I am Supposing Your Event is not calender event in android.
You can have one table with days and respective int values for it [same as java anum in the reference question link].
Day | Value
Monday | 1
Tuesday| 2
.
.
.
.
And while storing event , store day integer values with event. (As you know days of each event)
And while querying , equate value in event table with the table above.
OR
You can store directly string values like "Monday","Tuesday",... in event table directly.

Android SQLite dealing with days

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.

Android content provider update certain columns

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

Get all dates with optional further data, if a condition is met (Android - SQLite)

What I have
I'm working on an Android app with a menu of school canteens. I download data from a remote website and save them to the internal SQLite database to such table (SO Markdown doesn't support tables, hence I'll make it a list):
ID
TEXT - The name of the meal
NUMBER - The number of the meal (we can choose from more)
ORDERED - Boolean, whether the particular meal is ordered or not
DATE - The date (timestamp) when the meal is server
(and a few more unimportant columns)
What I need
I want to provide the user with a list, where for every day when the canteen cooks there is either the meal he has ordered or information, that he hasn't ordered anything for that day.
The question is, how should look my query when I need to get:
Every date that is in the table (once).
For each date either the info about the ordered meal, or (if there isn't any ordered meal) something from which I can tell that the user will be hungry that day.
Because I want my users to be able to see also the meals they didn't order (so that their friends can look, what they might have), I refuse to keep only the ordered meals.
Thanks in advance for any help.
SELECT *, MAX(Ordered) FROM MyTable GROUP BY "Date"
The GROUP BY "Date" ensures that you get one result per day; the MAX(Ordered) ensures that that result is one that is ordered, if possible.

Android - easy/efficient way to maintain a "cumulative sum" for a SQLite column

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).

Categories

Resources