Is there type Long in SQLite?

前端 未结 7 704
我在风中等你
我在风中等你 2020-12-23 20:00

I want to create a table with the column type Long instead of Integer. Is it possible?

相关标签:
7条回答
  • 2020-12-23 20:34

    His above link depicts a 64 bit Integer or Essentially a long, also see What is the difference between SQLite integer data types like int, integer, bigint, etc.?

    and Version of SQLite used in Android?

    0 讨论(0)
  • 2020-12-23 20:36

    SQLIte will set suitable integer width depend on your input

    0 讨论(0)
  • 2020-12-23 20:45

    the INTEGER can store the long but to get long from cursor should do like this :

    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);
    }
    

    i use this to store timestamp in sqlite

    0 讨论(0)
  • 2020-12-23 20:47

    I don't think there is type long. Either you can use INTEGER (or) Numeric. Here is link with supported data types http://www.sqlite.org/datatype3.html

    0 讨论(0)
  • 2020-12-23 20:49

    From the SQLite docs

    INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

    Since long is 8 byte and INTEGER can also save values of 8 bytes, you can use INTEGER.

    0 讨论(0)
  • Create the column as an INTEGER type:

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

    Put the long value in INTEGER column:

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

    Important: retrieve the value from cursor as LONG

    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_A, null);
    long value = cursor.getLong(0);
    
    0 讨论(0)
提交回复
热议问题