2010-06-29 124 views
17

我一直在使用Android上的SQLite,我想添加一個ArrayList到表中的列,然後作爲一個ArrayList取回數據。 arraylist是一個Longs列表。我注意到SQL有一個用於存儲BLOBS的選項,但是它看起來像我需要先將數組列表轉換爲byte [],然後才能將它作爲blob存儲在我的SQLite數據庫中。在Android中的SQLite數據庫中保存ArrayList

如果任何人有解決如何將數據列表保存到SQLite數據庫,將不勝感激。或者還有其他的選擇來保存我的數據,我應該考慮一下嗎?

回答

2

聽起來就像要序列化列表。 Here is a tutorial/intro添加到Java序列化API。

+5

鏈接是死的... – Groot 2013-11-20 03:00:52

+0

鏈接進入java.com/ – clu 2015-06-25 23:30:20

1

你必須做手工,經過列表中的每個項目,將其存儲在數據庫

for (long l : array<long>){ 
//change to byte here 
//Store in database here 
} 
7

請原諒我的野蠻抄襲我以前的答案BLOB vs. VARCHAR for storing arrays in a MySQL table之前將其更改爲字節。那邊的其他答案也非常貼切。

我認爲Con的方法可能比使用java序列化更好,因爲java的內置序列化將需要額外的字節,而非java應用程序將有更難處理數據。

public static void storeInDB(ArrayList<Long> longs) throws IOException, SQLException { 

    ByteArrayOutputStream bout = new ByteArrayOutputStream(); 
    DataOutputStream dout = new DataOutputStream(bout); 
    for (long l : longs) { 
     dout.writeLong(l); 
    } 
    dout.close(); 
    byte[] asBytes = bout.toByteArray(); 

    PreparedStatement stmt = null; // however you get this... 
    stmt.setBytes(1, asBytes); 
    stmt.executeUpdate(); 
    stmt.close(); 
} 

public static ArrayList<Long> readFromDB() throws IOException, SQLException { 

    ArrayList<Long> longs = new ArrayList<Long>(); 
    ResultSet rs = null; // however you get this... 
    while (rs.next()) { 
     byte[] asBytes = rs.getBytes("myLongs"); 
     ByteArrayInputStream bin = new ByteArrayInputStream(asBytes); 
     DataInputStream din = new DataInputStream(bin); 
     for (int i = 0; i < asBytes.length/8; i++) { 
      longs.add(din.readLong()); 
     } 
     return longs; 
    } 

} 

注意:如果您的列表有時包含超過31個長(248字節),那麼您將需要使用BLOB。你不能在MySQL中使用BINARY()或VARBINARY()。我知道你問的SQLite,但在完全抄襲我以前的答覆精神,我會假裝你問關於MySQL:

mysql> CREATE TABLE t (a VARBINARY(2400)) ; 
ERROR 1074 (42000): Column length too big for column 'a' (max = 255); 
use BLOB or TEXT instead 
6

我有兩個ArrayList<String>,既會1000 +條目。我查看了斑點和字節,但對於我來說,加速該過程並使其可用的解決方案是通過更改插入方法並擺脫database.insert - 此信用爲here

private static final String INSERT = "insert into " 
      + YOUR_TABLE_NAME+ " (" + COLUMN_1 + ", " 
      + COLUMN_2 + ") values (?, ?)"; 

public void insertArrayData(ArrayList<String> array1, 
      ArrayList<String> array2) { 

     try { 
      database.open(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
     int aSize = array1.size(); 

     database.beginTransaction(); 

     try { 
      SQLiteStatement insert = database.compileStatement(INSERT); 

      for (int i = 0; i < aSize; i++) { 

       insert.bindString(1, array1.get(i)); 
       insert.bindString(2, array2.get(i)); 
       insert.executeInsert(); 

      } 

      database.setTransactionSuccessful(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } finally { 
      database.endTransaction(); 
     } 

     try { 
      database.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

它很容易適應longs和整數等,並迅速減輕。所以謝天謝地,我不必再爲了斑點和字節而撓頭了!希望能幫助到你。

25

要插入:

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

//....Add值來inputArray

Gson gson = new Gson(); 

String inputString= gson.toJson(inputArray); 

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

使用 「inputString」 保存在SQLite數據庫的ArrayList值

中檢索:

從SQLiteDatabse中獲取您保存並更改爲ArrayList的字符串類型如下: outputarray是一個字符串,它是從SQLiteDatabase獲取的。

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

ArrayList<String> finalOutputString = gson.fromJson(outputarray, type); 
  • SQLite中使用文本格式存儲的字符串值.....
+1

是什麼GSON?沒有選擇這個課程。 – 2016-05-11 02:49:47

+2

@ Md.ImranChoudhury'Gson(https://github.com/google/gson)是一個使用反射將Java對象轉換爲JSON或從JSON轉換而來的庫。' – sandrstar 2016-06-20 00:31:27

+1

謝謝@sandrstar – 2016-06-20 16:35:25

3

有一種更簡單的方法,以完全另一種方式做這樣的事情。 您可以創建一個由所有數組值組成的字符串。 。 對於做出的StringBuilder,並用分離器(如simbole你,你不會在你的數組值連續使用和offcource追加值,例如虛擬「|」 在示例代碼:

double[] mylist = new double[]{23, 554, 55}; 
    StringBuilder str = null; 
    str = new StringBuilder(); 

    for(int i=0;i<mylist.length;i++){ 
     str.append(mylist[i]+"|"); 
    } 

    String result = str.toString(); 

    db.open(); 
    db.insert(result); 
    db.close(); 

當你想要獲取它們並使用這些值時,從數據庫中獲得純數據到字符串並且使用splite()操作純數組的列中每個數組的值比u可以輕鬆地使用它:)

讓我們在代碼中做:

String str = db.getdata(); 
    String[] list = str.split("|"); 

用簡單的轉換就可以使用它們作爲double;

double mydouble = Double.parsDouble(list[1].toString()); 

也許它不是標準,但它是有益的,希望幫助;)

0
ArrayList<String> list = new ArrayList<String>(); 
        list.add("Item 1"); 
        list.add("Item 2"); 
        list.add("Item 3"); 

        String joined = TextUtils.join(",", list); 
        Log.i(TAG, "joined strings: " + joined); 

        String[] array = TextUtils.split(joined, ","); 
        Log.i(TAG, "joined strings: " + array[0] + array[1] + array[2]);