Is there type Long in SQLite?

AndroidSqlite

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

Solution 3 - Android

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

Solution 4 - Android

Solution 5 - Android

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

Solution 6 - Android

SQLIte will set suitable integer width depend on your input

Solution 7 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex KapustianView Question on Stackoverflow
Solution 1 - AndroidInder Kumar RathoreView Answer on Stackoverflow
Solution 2 - AndroidVasil ValchevView Answer on Stackoverflow
Solution 3 - AndroidkosaView Answer on Stackoverflow
Solution 4 - AndroidL7ColWintersView Answer on Stackoverflow
Solution 5 - AndroidShnkcView Answer on Stackoverflow
Solution 6 - AndroidDarkTempleView Answer on Stackoverflow
Solution 7 - AndroidCrissView Answer on Stackoverflow