Search code examples
javaandroidsqlsqliteunix-timestamp

Storing unix timestamp in sql


I have a unix date in long format

Long tmpLong = (Long) local.getValue();

Here is the value of tmpLong

1485710457166

Now I want to store this variable inside of my SQLite DB, after some research I found out that you cant store Longs in SQLite so I tried this:

Long tmpLong = (Long) local.getValue();
Integer tmpInt = tmpLong.intValue();

But this gives me:

-348227250

In SQLite my datatype is INTEGER

I need help storing this unix number, any help is appreciated

Update 1

If I try store it as a long, so

localDB.add(image_id, unixTimestamp, PATH);

And then in my add method (inside SQLiteHelper class). I print the value for testing :

 public void add(String image_id, Long date, String path) {
    SQLiteDatabase db = this.getWritableDatabase();
    Log.d("SQL", "date " + date);

It prints 1

D/SQL: date 1


Solution

  • Well, that is what will happen when you truncate a 64 bit Long into a 32 bits integer...

    Dont do that but try to store it like it is (as a long) because SQLite ints can handle that values, take a look into the Datatypes In SQLite

    enter image description here

    Edit:

    Your code below:

    public void add(String image_id, Long date, String path) {
        SQLiteDatabase db = this.getWritableDatabase();
        Log.d("SQL", "date " + date);
    }
    

    if that code is so as it is posted then you are passing as parameter 1 for the date value... the method this.getWritableDatabase(); is not going to modify/change the value of the date parameter...