Android content provider modified / created date - java

In example: If I'm accessing the SMS database, there is a 'Date' field for message timestamp. However, say I load messages with an older timestamp into the database, how do I know that they were added after the later timestamped messages? Is there a modified or created date for table rows that I can access?
Edit and clarification:
The SMS table has the following columns: long _id, thread_id, address, person, date, protocol, read, status, type, reply_path_present, subject, body, service_center, locked,error_code, & seen.
'date' refers to the date which the message was sent, not when it was added to the database. Does the Android database (SQLite) track when data is added or modified in the database? If so, how do I access it? If it does not, and simply add/removes/updates data without logging, that's an answer too.

you can solve this problem by creating a column containing the current date and time (current means at the moment, the row was inserted):
create table mytable(_id int primary key, date datetime default current_timestamp);
this will result in a timestamp in every row that points to the moment of insertion
another solution would be simply insert the current timestamp out of the SQLiteOpenHelper like:
ContentValues values = new ContentValues(2);
values.put(COLUMN_NAME_ID, id);
values.put(COLUMN_NAME_TITLE, title);
values.put(COLUMN_NAME_CREATED_DATE, System.currentTimeMillis());
dbHelper.getWritableDatabase().insert(TABLE_NAME, null, values);

There is no date in sqlite for when the row was inserted or updated.

Related

SQL/JPA want to select data for a list of ids filtering by a specific timestamp range for each id

I have a table with some data, there are record id, userIds, timestamp and data columns. Receive from the client a list of userIds, initially, I just had to fetch data by the same timestamp range for all useIds, just using userId IN (list). However, now I'm required to get data by different timestamp ranges for each userId, let's say userID 1 needs data from 1643580000000 to 1646431200000 and userId 2 from 1626418800000 to 1647500400000 (utcTimestamp in mills).
Most probably I'll receive a list of [userId, startTime, endTime], so, I was considering to loop the main query for each userId with its respective timestamp range (I've seen it's a bad idea due to performance, but if I have to, I have to), but I also found out about cursors (not much experience here).
I wanted to know if it's possible to get what I want without loops or cursors, and if not, best way with each one.
Thanks in advance!
Notes: using MariaDB. SQL query will be used as nativeQuery in a Java service repository.
You can run a query laike this.
Of course you have to add for every user his own time range
SELECT * FROM mytable
WHERE (userID = 1 AND `timestamp` BETWEEN 1643580000000 AND 1646431200000)
OR (userId = 2 AND `timestamp`BETWEEN 1626418800000 AND 1647500400000)
If you have a lot of rows and not that many id to process, you can do it with
SELECT * FROM mytable
WHERE (userID = 1 AND `timestamp` BETWEEN 1643580000000 AND 1646431200000)
UNION
SELECT * FROM mytable
WHERE (userId = 2 AND `timestamp`BETWEEN 1626418800000 AND 1647500400000)
Here also you need to add for every id another UNION

HQL query ignores time portion of the Date object

I have problem with HQL where I am setting the query parameters. One of them is Date. When I debug the code there is Date with time entering the method. I set the parameter using setParameter(timestamp, new Timestamp(date.getTime())) or query.setTimestamp...etc etc I used many combinations...
When I use p6spy to examine the SQL comming from app to the DB there is only '29-Jan-21' or other date without time.
I am using hibernate 5.1.0 final and postgre DB. I'll be glad for any help.
Example:
Query query = getSessionFactory().getCurrentSession().createQuery("SELECT user FROM UserEntity cr WHERE user.userStatus.id = :statusId AND :timestamp >= user.valid_to");
This is how I tried to set the timestamp parameter:
query.setParameter("timestamp", new Timestamp(date.getTime()));
query.setParameter("timestamp", date, TimestampType.INSTANCE);
query.setTimestamp("timestamp", date);
query.setTimestamp("timestamp", new Timestamp(date.getTime()));
Problem is that the generated SQL replace timestamp by '29-Jan-21' or other date I choose but without time. The date parameter comes to the method from UI and it contains full date with time.

Update timestamp in dynamoDB

I want CREATE_DATE and LAST_UPDATED fields in DynamoDB table. It is creating those fields with initial values for both fields as current date. But when some of the columns get updated it does not update the LAST_UPDATED column with new date.
I am using DynamoDBAutoGeneratedTimestamp annotation and both CREATE_DATE and LAST_UPDATED are of type java.util.date
#DynamoDBAutoGeneratedTimestamp(strategy=DynamoDBAutoGenerateStrategy.CREATE)
private Date CREATE_DATE;
#DynamoDBAutoGeneratedTimestamp(strategy=DynamoDBAutoGenerateStrategy.ALWAYS)
private Date LAST_UPDATED;
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBAutoGeneratedTimestamp.html.
Can anyone tell what else needs to be added if I am missing anything ?
Or I need to handle update date in code and it would not be auto populated?
Thank you

compare date from database with value i get from inputtext in java jsp and sql

I have a table , there are 4 fields: name, start date and time (Timestamp) ,end date and time (timpestamp) , car.
In Java/MySQL,
I need to compare database start date time and end start date time and compare with value given in textbox date time field .
Now problem is that we need to book person(driver) and car, if car and person
are not booked in given time (that is checked by database) , then we can booked it else not.
Please tell me a logic/query to do this.
If you have code please mention it.
Try this query,
SELECT name FROM table having '2014-11-28' between start_date and end_date;
You can use this query to check availability.
SELECT name FROM table WHERE start_date < '2014-03-13 11:42:28' AND end_date > '2014-03-13 11:42:28' limit 1
If you get any name, then you can not book new driver. Also look at mysql date and time functions.

How to create log activity from java to MySQL?

I want to create a log activity for each user when they login from my apps and insert into MySQL database with the structure like
id_log (int, primary key, auto increment)
username (varchar)
time (timestamp)
The problem is every pc has a different timestamp and I only know to get timestamp on a local machine, is there any way to create a log activity based on a timestamp from a PC that storing database? I ask the other and said to use log4j but I still don't get it.
You can use MySQL's NOW() function, but the TIMESTAMP datatype automatically initialises to the current time by default:
CREATE TABLE logs (
id_log SERIAL,
username VARCHAR(255),
time TIMESTAMP
);
INSERT INTO logs (username) VALUES ('eggyal');
See it on sqlfiddle.

Categories

Resources