Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cursor getInt return negative value

at first I want to notice that english isn't my native language, whatever I hope we won't have missunderstaning

I write simple application on Android, and found an issue, also solved it, but anyway I want to understand why it was.

I created an sql table:

public static final String CREATE_TABLE_ENTRIES = " CREATE TABLE "
            + TABLE_ENTRIES + " ( " + KEY_ID + " TEXT PRIMARY KEY, "
            + " content " + " TEXT, "
            + " title" + " TEXT, "
            + " image " + " TEXT, "
            + " date " + " INTEGER, " + ")";

And put an data into it this way:

SQLiteDatabase db = mStorage.getWritableDatabase();
ContentValues tempValues = new ContentValues();
tempValues.put("id", entry.id);
tempValues.put("content", entry.content);
tempValues.put("title", entry.title);
tempValues.put("image", entry.image);
tempValues.put("date", entry.date);  // int date; I use Unix time
db.insert(contentType, null, tempValues);

Now.. everything is ok, I verified using this application, that stored values are allright.

Nevertheless when I try to get the date value using cursor.getInt(cursor.getColumnIndex("date")) I'm getting 1) wrong value 2) its negative ( like -1004124 )

So, I tried to replace it with getLong and voila! I get my unix time.

The question is - whats wrong with getInt here?

like image 814
kio_tk Avatar asked Aug 27 '26 16:08

kio_tk


1 Answers

Are you sure you're using unixtime (seconds since epoch) and not e.g. Java system time (milliseconds since epoch)?

For example, unixtime 1387233645 fits well in a 32-bit int.

However, millisecond stamp 1387233645000 is too large for 32 bits. In hex it's 0x142fd9191c8. Taking the bottom 32 bits leaves us with 0xfd9191c8 which is -40791608 in decimal when interpreted as a Java int i.e. signed two's complement 32-bit integer.

like image 155
laalto Avatar answered Aug 30 '26 08:08

laalto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!