在 SQLite 数据库中保存 ArrayLists [英] Saving ArrayLists in SQLite databases

查看:76
本文介绍了在 SQLite 数据库中保存 ArrayLists的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想保存一组有序的 double 值,并且我希望能够轻松地插入、检索或删除任何值.因此,我使用了一个 ArrayList,我在其中定义了一个名为 Doubles 的类来存储双精度值.

So I want to save an ordered set of double values, and I want to be able to insert, retrieve or delete any value from this easily. As of such, I'm using a an ArrayList, where I define a class called Doubles to store the double values.

如何将此数组列表存储在 SQLite 数据库的记录中?我的意思是……列类型应该是什么?可以吗?

How do I store this arraylist in a record in an SQLite database? I mean...what should the columns type be? Can it be done?

推荐答案

不能将 ArrayList 直接插入 Sqlite.相反,您可以使用 JSONObject (org.json.JSONObject) 来插入 ArrayList.请检查以下代码段,您可以尝试以下操作....

You cannot insert ArrayList directly into Sqlite. Instead, you could use JSONObject (org.json.JSONObject) to insert the ArrayList. Please check below snippet, you can try something like below....

插入,

JSONObject json = new JSONObject();
json.put("uniqueArrays", new JSONArray(items));
String arrayList = json.toString();

将字符串插入数据库中.

Insert the string into db.

阅读,从 db 中读取字符串作为字符串,

To Read, Read the string from db as String,

 JSONObject json = new JSONObject(stringreadfromsqlite);
  ArrayList items = json.optJSONArray("uniqueArrays");

这篇关于在 SQLite 数据库中保存 ArrayLists的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆