150

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

random_user
  • 820
  • 1
  • 7
  • 18
Alex K
  • 5,092
  • 15
  • 50
  • 77

7 Answers7

249

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.

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
22

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);
Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
Vasil Valchev
  • 5,701
  • 2
  • 34
  • 39
18

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

kosa
  • 65,990
  • 13
  • 130
  • 167
5

You just define a column with Integer type. SQLite sets the column length to 1,2,4,8 depending on your input.

Shnkc
  • 2,108
  • 1
  • 27
  • 35
5

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?

Community
  • 1
  • 1
L7ColWinters
  • 1,342
  • 1
  • 14
  • 31
4

SQLIte will set suitable integer width depend on your input

DarkTemple
  • 71
  • 4
1

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

Criss
  • 755
  • 7
  • 22