在Android的SQLite数据库中保存ArrayList [英] Saving ArrayList in SQLite database in Android
问题描述
我一直在 android 上使用 SQLite,我想向表中的列添加一个数组列表,然后将数据作为数组列表取回.arraylist 是一个 Long 列表.我注意到 SQL 有一个用于存储 BLOBS 的选项,但是看起来我需要先将 arraylist 转换为 byte[],然后才能将其作为 Blob 存储在我的 SQLite 数据库中.
I've been working with SQLite on android and I would like to add an arraylist to a column in a table, and then fetch the data back as an arraylist. The arraylist is a list of Longs. I've noticed that SQL has an option for storing BLOBS, however it looks like I need to convert the arraylist to a byte[] first before being able to store it as a blob in my SQLite database.
如果有人有关于如何将数组列表保存到 SQLite 数据库中的解决方案,我们将不胜感激.或者有没有其他选项可以保存我的数据数组,我应该考虑?
If anyone has a solution on how to save arraylists into an SQLite database that would be greatly appreciated. Or is there any other option for saving my array of data, i should consider?
推荐答案
插入:
ArrayList<String> inputArray=new ArrayList<String>();
//....向 inputArray 添加值
//....Add Values to inputArray
Gson gson = new Gson();
String inputString= gson.toJson(inputArray);
System.out.println("inputString= " + inputString);
使用inputString"在SQLite数据库中保存ArrayList的值
use "inputString" to save the value of ArrayList in SQLite Database
检索:
从 SQLiteDatabse 中获取您保存并更改为 ArrayList 类型的字符串,如下所示:outputarray 是一个从 SQLiteDatabase 获取的字符串,用于本示例.
Get the String from the SQLiteDatabse what you saved and changed into ArrayList type like below: outputarray is a String which is get from SQLiteDatabase for this example.
Type type = new TypeToken<ArrayList<String>>() {}.getType();
ArrayList<String> finalOutputString = gson.fromJson(outputarray, type);
- 在 SQLite 中使用文本作为格式来存储字符串值......
这篇关于在Android的SQLite数据库中保存ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!