Android SQLite dealing with days - android

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.

Related

Efficiently storing & querying integer arrays in SQLite

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

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 working with dates and SQLite

I am trying to create a simple reminder app. Im just logically thinking about the process. Basically I want to be able to choose a DAY and time e.g Monday 15:00, this will trigger EVERY Monday 15:00 until it gets deleted from database. Having said that example I have questions to accomplish this process.
How will I store DAY and TIME, what type, do I need different columns in my table?
How can I compare real time DAY to current DAY, so if its Monday real time it will return ONLY Monday reminders? is this possible?
Will I need to primarly focus using calendar?
As documentation says:
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values.
You can store date and time in the TEXT type of field in the following format:
YYYY-MM-DD HH:MM:SS.SSS
and then use built-in date & time functions of SQLite to filter your records.
Another option is to store date & time as a time-stamp in milliseconds and write proper queries for selecting data or use Joda library for filtering or date & time transformation, but probably such solution would be less efficient and less convenient than first option.
Using integer column is the easiest solution.
Just store the date in millisecond (Calendar.getTimeInMillis()) and your good to go.
Then you just have to search on that integer to find the correct event in your database :
String selectQuery = "SELECT whateveryouneed FROM events WHERE date_event > ?";
Cursor c = db.rawQuery(selectQuery, new String[] { String.valueOf(calendar.getTimeInMillis())});
...
if you need to find all the event for a day , you just have to find the limits of the day in millisecond and make a query according to those limits

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

How to get the number of days between a given date and the previous records date?

I have a ListView with records and tapping on a record opens a detailed record view. In this detailed view, I want to print out the number of days between the current record and the previous record. How can I do that?
I know there exist an SQLite function getting the last inserted id, but I can't see that it would help here.
I'm thinking of solving it this way:
Get the id of the current record shown in detailed view
Get its date and save temporarily
Given that id, what is the id of the record written before current id?
Given the id of the previous record, get its date and save temporarily.
Calculate number of days between current record in detailed view and previous record.
So, the answer I want is like: this tank lasted for 13 days.
Actually I'm stuck on step 3 above, how can I find out, given an arbitrary id, what the record stored in the db before that id, has for id?
An example:
id date note
---------------------------
1 2013-01-03 first note
2 2013-01-13 second note
4 2013-01-20 ...
7 2013-02-01 ...
If I open the detailed view for record with id 4, I want to calculate the number of days gone since record with id 2 was written. I.e. 2013-01-20 - 2013-01-13.
Kind regards, Ramon
You can use the following approach for date manipulation:-
SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd");
long d1=formater.parse(date1Str).getTime();
long d2=formater.parse(date2Str).getTime();
System.out.println(Math.abs((d1-d2)/(1000*60*60*24)));
You should not go by ID. Insert the timestamp while inserting record. Get the record order by timestamp.
Once you have ordered data, it is just matter of writing date difference function.

Categories

Resources