将ArrayList / Vector插入SQLite DB

时间:2012-01-22 19:11:54

标签: android database sqlite arraylist

我有一个具有这种结构的ArrayList或Vector

ArrayList = value1 value2 value3 value4 value5, value1_1 value2_1 value3_1 value4_1 value5_1, [...]

最多为value1_200(变量,但是此大小)。

我想在一列中插入每个值。我的数据库结构:

value1 VARCHAR, value2 VARCHAR, value3 VARCHAR, value4 VARCHAR, value5 VARCHAR

我应该使用BLOB吗?如何将其插入SQLite数据库? (每个值集应该分成一行)

1 个答案:

答案 0 :(得分:0)

自行解决,将ArrayList转换为String并使用此方法插入。

  int init = 0;
  for(int t = init; t < arr.length; t++){
      Log.i(TAG, "arr["+t+"] = " + arr[t].trim());

    if(r == 1) {
        database.execSQL("INSERT INTO table (id) VALUES ('" + arr[t].trim() + "')");
        id_save = arr[t].trim();
        r++;
    }else if(r == 2) {
        database.execSQL("UPDATE table SET name='" + arr[t].trim() + "' WHERE id='" + id_save + "'");
        name_save = arr[t].trim();
        r++;
    }else if(r == 3) {
        database.execSQL("UPDATE table SET weburl='" + arr[t].trim() + "' WHERE id='" + id_save + "' AND name='" + name_save + "'");
        r++;
    }else if(r == 4) {
        database.execSQL("UPDATE tables SET mainurl='" + arr[t].trim() + "' WHERE id='" + id_save + "' AND name='" + name_save + "'");
        r++;
    }else if(r == 5) {
        database.execSQL("UPDATE table SET logo='" + arr[t].trim() + "' WHERE id='" + id_save + "' AND name='" + name_save + "'");
        r = 1;
    }