SQLite中是否有Long类型?

时间:2011-12-29 19:29:05

标签: android sqlite

我想创建一个列类型为Long而不是Integer的表格。可能吗?

7 个答案:

答案 0 :(得分:200)

来自SQLite docs

  

<强> INTEGER 即可。该值是有符号整数,存储为1,2,3,4,6或8个字节,具体取决于值的大小。

由于long为8字节且INTEGER也可以保存8字节的值,因此您可以使用INTEGER

答案 1 :(得分:18)

将列创建为INTEGER类型:

db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_A + "("
                + KEY + " INTEGER" + ")");

long值放在INTEGER列中:

contentValues.put(KEY, object.getLongValue());
db.insert(TABLE_A, null, contentValues);

重要提示:将光标中的值检索为LONG

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

答案 2 :(得分:14)

我认为类型不长。您可以使用INTEGER(或)Numeric。以下是与支持的数据类型http://www.sqlite.org/datatype3.html

的链接

答案 3 :(得分:5)

您只需使用整数类型定义一列。 SQLite根据您的输入将列长度设置为1,2,4,8。

答案 4 :(得分:4)

他的上面的链接描绘了64位整数或基本上很长,也看到了 What is the difference between SQLite integer data types like int, integer, bigint, etc.?

Version of SQLite used in Android?

答案 5 :(得分:3)

SQLIte将根据您的输入设置合适的整数宽度

答案 6 :(得分:0)

INTEGER 可以存储 long ,但是要想从 cursor 获取数据,应该这样做:

int type = cursor.getType(j);

String value = cursor.getString(j);
try {
       int intValue = Integer.parseInt(value);
       field.set(object, intValue);
    } catch (NumberFormatException e) {
       long longValue = Long.parseLong(value);
       field.set(object, longValue);
}

我用它在 sqlite

中存储时间戳