【问题标题】:Saving ArrayList in SQLite database in Android在 Android 的 SQLite 数据库中保存 ArrayList
【发布时间】:2011-03-09 17:01:01
【问题描述】:
我一直在使用 android 上的 SQLite,我想将数组列表添加到表中的列中,然后将数据作为数组列表取回。 arraylist 是一个 Long 列表。我注意到 SQL 有一个存储 BLOBS 的选项,但是看起来我需要先将 arraylist 转换为 byte[],然后才能将它作为 blob 存储在我的 SQLite 数据库中。
如果有人有关于如何将数组列表保存到 SQLite 数据库中的解决方案,将不胜感激。或者我应该考虑保存我的数据数组的其他选项吗?
【问题讨论】:
标签:
java
sql
android
sqlite
arraylist
【解决方案1】:
插入:
ArrayList<String> inputArray=new ArrayList<String>();
//....将值添加到 inputArray
Gson gson = new Gson();
String inputString= gson.toJson(inputArray);
System.out.println("inputString= " + inputString);
使用“inputString”将ArrayList的值保存在SQLite数据库中
检索:
从 SQLiteDatabse 中获取您保存的字符串并更改为 ArrayList 类型,如下所示:
outputarray 是一个从 SQLiteDatabase 获取的字符串。
Type type = new TypeToken<ArrayList<String>>() {}.getType();
ArrayList<String> finalOutputString = gson.fromJson(outputarray, type);
- 在 SQLite 中使用文本格式存储字符串值.....
【解决方案2】:
请原谅我野蛮地抄袭我之前对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 个 long(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
【解决方案3】:
我有两个ArrayList<String>,都有 1000 多个条目。我查看了 blob 和字节,但对我来说,加快进程并使其可用的解决方案是通过更改插入方法并摆脱 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 和 Integers 等并且快速减轻。所以谢天谢地,我不必再为 blob 和字节而挠头了!希望能帮助到你。
【解决方案4】:
有一种更简单的方法可以完全以另一种方式来做这样的事情。
您可以创建一个包含所有数组值的字符串。
为此,请创建一个 StringBuilder 并连续附加值并使用分隔符在场外添加(就像您不会在数组值中使用的符号一样。例如 virtual '|' 。
以代码为例:
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() 操作将数组中的每个值都放在数组的列中,这样你就可以轻松地使用它了:)
让我们用代码来做吧:
String str = db.getdata();
String[] list = str.split("|");
通过简单的转换,您可以将它们用作双精度;
double mydouble = Double.parsDouble(list[1].toString());
也许它不是标准的,但它是有帮助的,希望帮助;)
【解决方案6】:
您必须手动完成,遍历列表中的每个项目并将其更改为字节,然后再将其存储到数据库中
for (long l : array<long>){
//change to byte here
//Store in database here
}
【解决方案7】:
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]);