游标getInt返回负值 [英] Cursor getInt return negative value

查看:95
本文介绍了游标getInt返回负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

起初我想注意到英语不是我的母语,无论我希望我们不会误会

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

我在Android上编写了一个简单的应用程序,发现了一个问题,也解决了该问题,但是无论如何我都想了解为什么.

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

我创建了一个sql表:

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.

尽管如此,当我尝试使用cursor.getInt(cursor.getColumnIndex("date"))获取日期值时,我得到了1)错误的值2)其负数(例如-1004124)

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 )

因此,我尝试用getLong和voila替换它!我得到我的Unix时间.

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

问题是-这里的getInt有什么问题?

The question is - whats wrong with getInt here?

推荐答案

您确定使用的是unixtime(自纪元以来的秒数),而不是Java系统时间(自纪元以来的毫秒数)?

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

例如,unixtime 1387233645非常适合32位整数.

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

但是,毫秒戳1387233645000对于32位而言太大.以十六进制表示的是0x142fd9191c8.取低32位会留下0xfd9191c8,当解释为Java int时即为十进制的-40791608,即带符号的二进制补码32位整数.

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.

这篇关于游标getInt返回负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆