Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQlite creating a database with a timestamp column and adding a value in

I am trying to create a database that called rawData. The db will hava a column for the id, a foreign user id (_id from another table), data and finally a timestamp.

My question is how can I create a timestamp in SQlite and store it in the db also what type should the column be, text? the database needs to be able to store 150 float values a second and time stamp each of those 150 entries. Additionally since SQlite doesn't have a float type should i use real as the column type?

public class RawDatabase{
public static final String TABLE_RAW_DATA = "rawData";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_FOREIGN_USER_ID = "foreignUserId";
public static final String COLUMN_DATA = "data";
public static final String COLUMN_TIME_STAMP = "timeStamp";

// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
    + TABLE_RAW_DATA + "(" + COLUMN_ID
    + " integer primary key autoincrement, " + COLUMN_FOREIGN_USER_ID
    + " integer, " + COLUMN_DATA 
    + " real, " + COLUMN_TIME_STAMP
    + " text not null);";
}
like image 912
wyatt Avatar asked May 16 '13 19:05

wyatt


People also ask

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.

How do you add a timestamp to a database?

While inserting data in tables, sometimes we need to capture insert timestamp in the table. There is a very simple way that we could use to capture the timestamp of the inserted rows in the table. Capture the timestamp of the inserted rows in the table with DEFAULT constraint in SQL Server.

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. SSS").


1 Answers

The documentation says:

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.SSS").
  • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
  • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

If you need only seconds precision, use integers in Unix Time format. Otherwise, use floating-pointer numbers for fractional seconds.

like image 127
CL. Avatar answered Oct 18 '22 16:10

CL.