Best practice to store days in sqlite - android

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.

Related

SQLite in Android - How to generate User IDs with date and sequential number per day

I currently have an app where I store user data in a SQLite database, and one of my fields is a User ID. I would like to add an option to auto-generate User IDs in an mmddyyXXX format, where XXX is a sequential number per user that resets every day.
Does anyone know how I would approach this? I looked at some of the other similar questions, but they don't seem to be helpful.
This is not complicated at all. If your'e similar with SQLite in android just take the date and the userId using a SELECT and generate that string yourself.
If the XXX is not the userId just save another table containing 'tokens' for users. every userId would have a 'token'.
Every new day just change the contents of this table.
I believe you could use a TRIGGER that will generate the userid when a row is inserted.
The following may suit :-
CREATE TRIGGER IF NOT EXISTS newuserid AFTER INSERT ON users
BEGIN
UPDATE users SET userid = strftime('%m%d',date('now'))||substr(strftime('%Y',date('now')),3)||
(
SELECT CAST(substr('000',1,3-length(count()+1)) AS TEXT)||CAST((count()+1) AS TEXT)
FROM USERS
WHERE substr(userid,1,6) = strftime('%m%d',date('now'))||substr(strftime('%Y',date('now')),3)
)
WHERE userid IS NULL;
END;
The trigger is named newuserid
userid is the column for the auto-generated id. The above relies upon it being NULL so it cannot be a PRIMARY INDEX.
There is no reliance upon other columns.
Testing
Starting with an empty table :-
Inserting 4 rows using INSERT INTO users VALUES(null,'This is a new user'); results in :-
To check for another date the rows are adjusted from 041018??? to 040918??? as per :-
4 more rows are inserted using INSERT INTO users VALUES(null,'This is a new user');, resulting in :-
Note this answer isn't intended to be fail-safe but rather the basis of the concept for the answer.

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

How to know changing data of postgresql in last 5 seconds

I am developing an Odoo 8 (OpenERP) application. That application use postgresql database. In backend of Odoo 8, there is add sale order button. So, I want to know, how to know the changing data in the last 5 seconds? My need is I want to insert data from mobile apps. What tables that that changing? Any query for do that? Or another suggest.
the database has 314 tables. Is there application of part3 like MONYog may be?
Any help is very appreciate.
For this kind of situation, there is one best way to manage as
Use always two columns in each table as "CreatedOn" and "LastUpdatedOn", and insert proepr values in it as on create time add current time in both then on every update just change last updated on by current time so you will easily get data as per your requirement.
You could also add a function like this:
create or replace function notify_table_change() RETURNS TRIGGER
LANGUAGE PLPGSQL AS
$$
NOTIFY all_writes TG_RELNAME;
IF TG_OP = 'DELETE' THEN RETURN OLD; ELSE RETURN NEW; END OF;
$$;
Then you could add the trigger for all inserts, updates, and deletes (but not truncate since I didn't handle that).
Then te client can:
LISTEN all_writes;
And will get live notifications of which tables are updated in real time.
Monitoring database feels a bit strange approach to the problem. Writing custom modules for odoo (and OpenERP) is very simple and staightforward. I'd create module which triggers whatever you want to do.
Here is a brief example of simplest OpenERP / odoo module:
from osv import osv
class my_custom_module(osv.osv):
_inherit = 'sale.order'
_name = 'sale.order'
def create(self, cr, uid, vals, ctx={}):
<your code here, whatever you want to do when new sale.order obejcet is created >
return super(my_custom_module, self).create(cr, uid, vals, ctx)
my_custom_module()
Does this help?
In a live commercial app. The method I use is a creation and update date/time stamp column.
Example
create table foo (
foo_id serial not null unique,
created_timestamp timestamp not null default current_timestamp,
updated_timestamp timestamp not null default current_timestamp
) with oids;
The trigger to do the work
CREATE TRIGGER check_table_to_do_update
--What i want to do
The trigger on the table
CREATE TRIGGER check_update_foo
AFTER UPDATE ON foo
FOR EACH ROW
WHEN (OLD.updated_timestamp IS DISTINCT FROM NEW.updated_timestamp)
EXECUTE PROCEDURE check_table__to_do_update();
Anything else can put an unnecessary overhead on the system.
All the best

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.

SQLite in Android: is there a way to use a limited number of rows in my application, or should I remove them by hand?

I am currently building a database recording events on the phone, but as I don't want to make this a huge database, 100 events are more than enough.
This will keep my database light en efficient.
Unfortunately, I don't see a way to limit the number of rows other than
String sql = "DELETE FROM myTable WHERE _id <= "+limitId;
and I could run this code when the user launch/leaver the app, but I am expecting a better way to achieve this
Is there a more convenient way to achieve this?
If you are using a ContentProvider, you can implement your DELETE in onInsert, deleting a single row on every insert of a single row:
String sql = "DELETE FROM myTable WHERE _id IN (SELECT min(_id) FROM myTable)";
I guess you mean limit to the 100 newest events ?
if so, there is no better way to do it as you did: checking for entries on every insert and delete old entries if necessary.
It's just a matter of taste how or where you do your check, as flx mentioned you could do it in the ContentProvider or as you probably did in the BroadcastReceiver or Service where you actually add the new row. You could also set up a Trigger on your table, but the main idea remains the same. Here a link if you're interested in triggers:
http://www.tutorialspoint.com/sqlite/sqlite_triggers.htm

Categories

Resources