2011-04-18 142 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(); 
相关问题