2011-04-18 147 views
14

所以我想保存一個有序的double值集合,並且我希望能夠輕鬆地插入,檢索或刪除任何值。就這樣,我使用了一個ArrayList,在那裏我定義了一個名爲Doubles的類來存儲double值。在SQLite數據庫中保存ArrayLists

如何將此數組列表存儲在SQLite數據庫的記錄中?我的意思是......柱子應該是什麼類型的?可以做到嗎?

回答

48

不能將ArrayList直接插入到Sqlite中。相反,你可以使用JSONObject(org.json.JSONObject)來插入ArrayList。請檢查下面的代碼片段,您可以嘗試像下面....

要插入,

JSONObject json = new JSONObject(); 
json.put("uniqueArrays", new JSONArray(items)); 
String arrayList = json.toString(); 

插入串入分貝。

閱讀, 從數據庫作爲字符串讀取的字符串,

JSONObject json = new JSONObject(stringreadfromsqlite); 
    ArrayList items = json.optJSONArray("uniqueArrays"); 
+0

這聽起來很有趣!我會檢查它,看看它是如何工作的。 – Achint 2011-04-18 13:49:15

+0

太棒了。 – leparlon 2014-03-06 20:23:08

+0

嗨,你可以更多地解釋關於stringreadfromsqlite。 – max 2014-05-31 14:56:52

0

我建議你經歷所有3 Notepad tutorials你想存儲你存儲在數據庫表中的值。您不會將實際的數組直接存儲在數據庫中的數據中。但實際上並不需要使用數組,而不是向數組中添加新項目,而是調用db插入方法

-1

創建它有一個內部類和幾乎任何記事本教程說一個dbHelper類。該類必須具有如下所示的插入方法: -

public long insertRows(ContentValues values, String tableName) { 
    long val = myDatabase.insert(tableName, null, values); 
    return val; 
} 

此方法會將值添加到表格行中。 之後,你可以從主活動調用此方法,並且自您使用光標相信你會調用該方法在for循環中

爲(I = 0; list.length();我++),或者可以是它的則爲list.size:P

{

//調用此方法

}

,並通過調用該方法保留在數據庫中增加值環

0

我需要在我的應用程序中做類似的事情,我有一個自定義類(Foo,Bar等),我有一個持久化到SQL的foo,bar等的ArrayList。我對SQL的知識並不強,但我會在這裏解釋我的方法以防萬一。我的理解是,要存儲任何類型的對象,您需要爲該對象類型定義一個特定的表,其中該表具有表示該對象內的基本類型的單獨列。此外,要持久並檢索這些對象的ArrayList,您將爲每個ArrayList條目使用一個錶行,並在循環中迭代以存儲和檢索。

在我的應用程序中有幾個自定義類的ArrayLists,我想堅持DB。所以,要使事情變得整潔(至少對我來說 - 我仍然是一個相對較新的Java/Android程序員,所以拿一點鹽),我決定實現一種「SQL Serializable接口」,這是我的必須實現DB持久化對象。每個對象(Foo,Bar等))可以堅持到DB必須執行:

  1. 公共靜態最終TABLE_NAME字符串,用於此對象類型的SQL DB表的名稱。
  2. 一個公共靜態最終TABLE_CREATE_STRING,一個完整的SQL指令爲這個對象創建表。
  3. 從ContentValues對象填充其成員變量的構造方法。
  4. 'get'方法從其成員變量中填充ContentValues。

因此,說我有對象Foo和酒吧ArrayLists。當數據庫第一次創建時,在我的DB助手類中,我調用Foo.TABLE_CREATE_STRING,Bar.TABLE_CREATE_STRING等來創建這些對象的表。

要填充我的ArrayList,我使用類似:

cursor = dbh.retrieve(Foo.TABLE_NAME); 
if(!cursor.moveToFirst()){ 
    return false 
} 
do{ 
    DatabaseUtils.cursorRowToContentValues(cursor, vales); 
    FooArrayList.add(new Foo(values)); 
} while(cursor.moveToNext()); 
12

要插入:

ArrayList<String> inputArray=new ArrayList<String>(); 

//....Add值來inputArray

Gson gson = new Gson(); 

String inputString= gson.toJson(inputArray); 

System.out.println("inputString= " + inputString); 


use "inputString" to save the value of ArrayList<String> in SQLite Database 

中檢索:

Get the Str從SQLiteDatabse中保存並更改爲ArrayList類型,如下所示:

outputarray是一個字符串,它是從SQLiteDatabase中爲此示例獲取的。

Type type = new TypeToken<ArrayList<String>>() {}.getType(); 

ArrayList<String> finalOutputString = gson.fromJson(outputarray, type); 
+1

謝謝!這有助於我的情況。 P.S.它適用於所有類型的ArrayList,不僅適用於 2015-04-23 15:27:07

+1

例如我使用ArrayList ,但我們可以使用其他數據類型,甚至可以將類型設置爲特定的類對象。 – Ashok 2015-04-25 09:24:50

+0

我得到一個錯誤...不能解析Gson ... :(..任何人都可以幫我這個嗎? – Benedict 2016-05-11 11:40:29

0

對我來說,這是POJO類的ArrayList的注意

private String mNoteTitle; 
private int mFingerIndex; 
private Point mNoteCoordinates; 

public Note(String noteTitle, int fingerIndex, Point noteCoordinates) { 
    this.mNoteTitle = noteTitle; 
    this.mFingerIndex = fingerIndex; 
    this.mNoteCoordinates = noteCoordinates; 
} 

由於手冊說的JSONObject只支持以下幾種類型:對象:一個JSONObject,JSONArray,字符串,布爾值,整數,長,雙, NULL或null。可能不是NaN或無限。所以,我應該將我的Note類分解爲支持的對象。

JSONObject json = new JSONObject(); 
    JSONArray jsonArray = new JSONArray(); 

    for(Note note: chordShape.getNotes()){ 
     JSONObject singleNoteJsonObject = new JSONObject(); 

     try { 
      singleNoteJsonObject.put(SHAPE_NOTE_TITLE, note.getNoteTitle()); 
      singleNoteJsonObject.put(SHAPE_NOTE_FINGER_INDEX, note.getFingerIndex()); 
      singleNoteJsonObject.put(SHAPE_NOTE_X, note.getNoteCoordinates().x); 
      singleNoteJsonObject.put(SHAPE_NOTE_Y, note.getNoteCoordinates().y); 
     } catch (JSONException e){ 
      e.printStackTrace(); 
     } 

     jsonArray.put(singleNoteJsonObject); 

    } 

將數組創建到JSONObject中。

try { 
     json.put(SHAPE_NOTES, jsonArray); 
     Log.i(TAG, json.toString()); 
    } catch (JSONException e){ 
     e.printStackTrace(); 
    } 

創建字符串。在ContentValues

String notesList = json.toString(); 

認沽創建的String,原因在我的情況下,它是Android應用

if(notesList.length() > 0){ 
     contentValues.put(DatabaseHelper.SHAPE_NOTES_LIST, notesList); 
    } 

當我要讀取SQLite數據庫值。

ArrayList<Note> notes = new ArrayList<>(); 
    while(cursor.moveToNext()){ 
     JSONObject jsonNotes = null; 
     try { 
      jsonNotes = new JSONObject(cursor.getString(cursor.getColumnIndex(DatabaseHelper.SHAPE_NOTES_LIST))); 
     } catch (JSONException e){ 
      e.printStackTrace(); 
     } 

     if(jsonNotes != null){ 
      Log.i(TAG, jsonNotes.toString()); 
      JSONArray jsonArray = jsonNotes.optJSONArray(SHAPE_NOTES); 
      for(int i = 0; i < jsonArray.length(); i++){ 
       Note note = null; 
       JSONObject arrayObject = null; 

       try { 
        arrayObject = jsonArray.getJSONObject(i); 
       } catch (JSONException e){ 
        e.printStackTrace(); 
       } 

       if(arrayObject != null){ 
        try { 
         note = new Note(
          arrayObject.getString(SHAPE_NOTE_TITLE), 
           arrayObject.getInt(SHAPE_NOTE_FINGER_INDEX), 
           new Point(
             arrayObject.getInt(SHAPE_NOTE_X), 
             arrayObject.getInt(SHAPE_NOTE_Y) 
           ) 
         ); 
        } catch (JSONException e){ 
         e.printStackTrace(); 
        } 
       } 


       if(note != null){ 
        notes.add(note); 
       } 
      } 
     } 
    } 
    cursor.close(); 
相關問題