kimv
kimv

Reputation: 1579

How to store an int[] or float[] in an SqLiteDataBase in Android?

I have all these Strings like "TEXT", "INTEGER" and so on for a text or an integer.

Is there any method to add an integer array or a float array?

Thanks!

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE = "CREATE TABLE " + TABLE + "("
            + ID + " INTEGER PRIMARY KEY,"
            + ACCOUNT + " TEXT,"
            + TYPE + " INTEGER,"
            + AMOUNT + " INTEGER,"
            + YEAR + " INTEGER,"
            + MONTH + " INTEGER,"
            + DAY + " INTEGER,"
            + EXTRA + " TEXT"  + ")";
    db.execSQL(CREATE_TABLE);
}

Upvotes: 1

Views: 3767

Answers (3)

urandom
urandom

Reputation: 1157

The more SQL way would be to create a one-to-many relationship, and store the arrays as entries on the 'many' table.

Upvotes: 2

Blaze Tama
Blaze Tama

Reputation: 10948

While waiting somebody giving a better answer, you might want to try mine.

I usually save a short array (never tried for long arrays like 100+ items) just with ,

For example :

1,3,5,6,7

And then i will use split method, and add those values to a List<Integer>

Upvotes: 2

Pankaj Kumar
Pankaj Kumar

Reputation: 82958

There is no way to store array. But you can do it other way.

To Store : Convert array with comma separated string and store that. (Read Convert String[] to comma separated string in java)

To Retrieve : Convert comma separated string into array. (Read How to split a comma-separated string?)

Upvotes: 3

Related Questions