Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert TimeStamp in SQLite for Android

Tags:

android

sqlite

I have the following fields

1> WorkName - Varchar

2> TimeStap 

I wanted to create a Table with the above fields.

  1. What will be the TimeStamp datatype

  2. How can I insert timestamp values to the table.

  3. 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?

like image 308
chiranjib Avatar asked Feb 01 '11 19:02

chiranjib


People also ask

Does SQLite have timestamp?

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.

How do I insert the current date and time in SQLite?

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.

How do I store time stamps in SQLite?

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.

What are the data types in SQLite?

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.


2 Answers

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.

like image 145
John J Smith Avatar answered Sep 19 '22 16:09

John J Smith


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;
    }
}
like image 20
phongnt Avatar answered Sep 20 '22 16:09

phongnt