1

I have a value from the epoch time like this 1549251913000, I save this value to SQLite. I create the table like the following:

CREATE TABLE TABLE_BOOKMARK (COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT, COLUMN_TITLE TEXT, COLUMN_SOURCE TEXT, COLUMN_DATEANDTIME INTEGER, COLUMN_GUID TEXT);

that value is COLUMN_DATEANDTIME but with INTEGER type. but when I take the value, it doesn't match what I expected. it becomes like this -1231280856. Please give me some advice, thanks

I have tried this solution but still mismatch when I get it from SQLite (seems it didn't work with my problem)

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Nanda Z
  • 1,604
  • 4
  • 15
  • 37

3 Answers3

4

Create a column in as INTEGER datatype put LONG value into INTEGER in column

Make sure when you retrieve the value from cursor as LONG

Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
long value = cursor.getLong(0);

For more : SQLite DataType Doc

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
2

Just to clarify the column type is, with one exception, largely irrelevant as any type of data can be stored in any type of column.

  • The exception is the rowid column or an alias of the rowid column, such a column MUST store an integer. By integer it is a 64bit signed integer and thus encompasses a java long.

The column type itself is also flexible for example CREATE TABLE mytable (mycolumn a_pretty_weird_column_type) is valid (as would be LONG). Such types are converted according to 5 rules to the column affinity.

Putting the above together, using :-

CREATE TABLE IF NOT EXISTS mytable (mycolumn a_pretty_weird_column_type);
DELETE FROM mytable;
INSERT INTO mytable VALUES
  (1549251913000),
    (999999999999999),('Fred'),(x'010203040506070809'),(0.234567),(null),
    ('999999999999999'), -- Note although specified as TEXT as mycolumn is effectively NUMERIC stored as INTEGER
    ('0.234567') -- As above but stored as REAL
;
SELECT 
    *,
    typeof(mycolumn) AS coltype, -- The column type (note as per value not column definition)
    hex(mycolumn) AS as_hex, -- Convert column to a hex representation of the data
    CAST(mycolumn AS TEXT) AS as_text, -- follow rules
    CAST(mycolumn AS INTEGER) AS as_integer,
    CAST(mycolumn AS REAL) AS as_real,
    CAST(mycolumn AS NUMERIC) AS as_numeric,
    CAST(mycolumn AS BLOB) AS as_blob
FROM mytable;

results in :-

enter image description here

The bible as such is Datatypes In SQLite Version 3.

but when I take the value, it doesn't match what I expected. it becomes like this -1231280856

The Issue

As such your issue is nothing to do with the column type, not SQLite as such, rather it's due to using the Cursor getInt method, instead of the getLong method.


One answer says,

Its always better to store date and time in form of Text in sqlite.

This is incorrect, from a space point of view and therefore the underlying efficiency the use of a numeric representation, i.e. 64bit signed integer as per SQL As Understood By SQLite - Date And Time Functions - Time Strings (maximum of 8 bytes), of the date and time will be more efficient than storing the 19 bytes (for an accuracy down to a second).

Community
  • 1
  • 1
MikeT
  • 51,415
  • 16
  • 49
  • 68
0

As per the docs sqlite does not have default storage class for date and time. Its always better to store date and time in form of Text in sqlite. You can then parse them into Date runtime whenever you want to use them