I have the following fields
1> WorkName - Varchar 2> TimeStap
I wanted to create a Table with the above fields.
What will be the TimeStamp datatype
How can I insert timestamp values to the table.
What will the timestamp value while inserting data or how to fetch the timestamp.
I have worked on SQLite but don't have any experience on adding TimeStamp as a field to the table & adding values to that.
What kind of CREATE
& INSERT
statements should I use?
Does SQLite have timestamp? 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: TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.
Inserting Date and DateTime data First, we need to import the datetime module and to get the current time and date information now() function can be used. Then they store the datetime information in a variable, so it can be used to insert datetime in the SQLite table.
Using INTEGER to store SQLite date and time values First, create a table that has one column whose data type is INTEGER to store the date and time values. Second, insert the current date and time value into the datetime_int table. Third, query data from the datetime_int table. It's an integer.
SQLite only has four primitive data types: INTEGER, REAL, TEXT, and BLOB. APIs that return database values as an object will only ever return one of these four types.
There are limited datatypes available in a Sqlite Database, so the one I find useful for dates is integer - this will accept long values (dynamically adjusts its size) so for dates store the milliseconds value of a date and the date can be easily reconsituted when reading the millisecond values back out of the database.
I create timestamp by this:
/**
*
* @return yyyy-MM-dd HH:mm:ss formate date as string
*/
public static String getCurrentTimeStamp(){
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTimeStamp = dateFormat.format(new Date()); // Find todays date
return currentTimeStamp;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With