I am developing an android application which is connected to the online database developed using mysql. I have provided the user to enter a time slot (ex. 10.00AM - 12.00PM), Day(Tuesday). And what I need is to delete the field when current date matches to the day and passed the time slot. (ex.Tuesday && 12.01PM).
How can I to write a query to delete the field automatically when the day and time matches the current day and time.
Thanks in Advance.
Write a cron job that will run at the required time to do the following:
ALTER TABLE `tablename` DROP COLUMN `columnname`
following query works fine for delete the record based on current timestamp.
DELETE FROM table
WHERE datecolumn = date('Y-m-d h:i A'); //'2012-05-05 10:00PM'
Related
I want to send some notification to android app when new row is inserted in table database, but I'm having trouble to check if there is new row inserted or not.
Is there any function or query to check if new row inserted in table? or some logic to acquire that.
It depends on the database you are using.
For example in MySQL you can query the table information_schema.tables to get the latest update time.
https://dev.mysql.com/doc/refman/8.0/en/information-schema-tables-table.html
For postgres
Check if track commit timestamp is on
show track_commit_timestamp;
If it's off, set it to on in your postgresql.conf file and restart postgres.
Post that run the following to track the latest updates in your table
SELECT pg_xact_commit_timestamp(xmin), * FROM YOUR_TABLE_NAME;
I am working on a daily devotional app but I need to get the right information on a problem.
I made the app offline where I set each day with a corresponding content. However, I want to correctly show set content for a day automatically when the user opens the app.
For example, if today was 21st of January, and the user opens the app it should immediately show contents for that day. How do I implement this. Any ideas would be appreciated.
Well you need to maintain DataBase (in SQL lite or ROOM (android)) so put your data into your database then whenever you needed just you need to call query to fetch data and show your content accordingly.
Follow step :-
Need to create DB and make your table related to content.
Put your data according to your requirement (means how you want to show on your content into your app)
Last when ever you open your app then you need to fire query from data base according to your date.
After fetching data from your query you just need to put data into your UI.
Room in android.
SQL in android
From where ever you are fetching content just pass the current date and get the data with respect to that date. If you are fetching content from local db you could use a simple sql query like :
"select * from content where date = " + date + " ;";
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.
I have a table named as Attendance, Here is the structure given below.
id fname lname roll_no date time
________________________________________________________
1 Qadir Hussain 08cs18 19/04/2013 8:45am
2 Qadir Hussain 08cs18 19/04/2013 8:50am
_______________________________________________________
i want to insert the record having roll_no = 08cs18 only one time per day. not more than once per day.
I can restrict this via if/else. but is it possible to restrict this via sqlite query?
Edit
Acctually i m making an app of studnets attendance, I m using the QR_code to scan encoded roll_no. once a student scan its card for the first time it should insert the record (i.e id = 1) if user again scan it on the same day it should not insert. means a particular student should have a attendance record only one time per day.
For both databases, make a unique key constraint on roll_no and date.
CREATE UNIQUE INDEX daily_roll_no ON Attendance (roll_no,date)
Then for mysql make your insert with the IGNORE clause. For sqlite, use OR IGNORE.
INSERT IGNORE INTO Attendance ....
INSERT OR IGNORE INTO Attendance ....
mysql:
ignore clause
create index
sqlite:
ignore clause
create index
You can do this in a single SQL:
INSERT OR REPLACE INTO <tablename>
(<identifier>, <time inserted>, column1, column2, ...)
SELECT <identifier>, <time inserted>,
COALESCE(column1,'new value 1'),
COALESCE(column2,'new value 2'), ...
FROM <tablename>
WHERE <identifier> = ...
AND <time inserted> > <now minus one day>
The basic idea is: using INSERT OR REPLACE you can update an existing row or insert a new one depending on one or more conditions. In this case I update the row with the same values, if the row is not older than one day.
If the line is not existing or older than one day, you can assign new values using COALESCE.
I know this is only a kind of outline but may be it helps though .... Cheers!
I have records in my SQLite database and I'd like to check if a certain record is older than a certain amount of hours (or minutes or seconds).
I couldn't find a function to calculate "age()" in SQLite (like they have in Postgres). Does it exist?
If not, I was already trying to extract the epoch of the record (works fine) and was thinking to compare that with the epoch of the current time. But I can't find a function that will simply return me the epoch of the current timestamp. Can anybody help?
(Note: I did check SimpleDateFormat, currenttimemillis() etc, but didn't seem to do what I want or I'm missing something)
You are able to retrieve difference between current time and "last_updated" only by SQL language:
SELECT strftime('%s','now','localtime')-strftime('%s','2011-08-18 22:49:00') as date;
This SQL statement will return me difference between current time and 2011-08-18 22:49:00. Instead of 2011-08-18 22:49:00 you can pass last_updated column name and it should return you difference between them.
You can read more here