Ach*_*int 17 database sqlite android
所以我想保存一组有序的双值,我希望能够轻松地插入,检索或删除任何值.就这样,我使用的是一个ArrayList,我在其中定义了一个名为Doubles的类来存储double值.
如何将此arraylist存储在SQLite数据库的记录中?我的意思是......列类型应该是什么?可以吗?
Suk*_*mar 53
您不能将ArrayList直接插入Sqlite.相反,您可以使用JSONObject(org.json.JSONObject)来插入ArrayList.请查看下面的代码片段,您可以尝试下面的内容....
要插入,
JSONObject json = new JSONObject();
json.put("uniqueArrays", new JSONArray(items));
String arrayList = json.toString();
Run Code Online (Sandbox Code Playgroud)
将字符串插入db.
要读取,请将db中的字符串作为String读取,
JSONObject json = new JSONObject(stringreadfromsqlite);
ArrayList items = json.optJSONArray("uniqueArrays");
Run Code Online (Sandbox Code Playgroud)
小智 14
要插入:
ArrayList<String> inputArray=new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
//添加值为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
Run Code Online (Sandbox Code Playgroud)
要撤消:
从SQLiteDatabse获取您保存的字符串并将其更改为ArrayList类型,如下所示:
outputarray是一个String,它是从SQLiteDatabase获取的示例.
Type type = new TypeToken<ArrayList<String>>() {}.getType();
ArrayList<String> finalOutputString = gson.fromJson(outputarray, type);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46355 次 |
| 最近记录: |