Kev*_*vik 55 database sqlite android json
如何在SQLite数据库中存储JSON对象?什么是正确的方法?
一个地方是blob类型列.如果我可以将JSON对象转换为字节数组并使用Fileoutputstream
另一个想法是将文本列存储为String
import org.json.JSONObject;
JSONObject jsonObject;
public void createJSONObject(Fields fields) {
jsonObject = new JSONObject();
try {
jsonObject.put("storedValue1", fields.storedValue1);
jsonObject.put("storedValue2", fields.storedValue2);
jsonObject.put("storedValue3", fields.storedValue3);
jsonObject.put("storedValue4", fields.storedValue4);
jsonObject.put("storedValue5", fields.storedValue5);
jsonObject.put("storedValue6", fields.storedValue6);
} catch (JSONException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
Pan*_*mar 75
将JSONObject转换为String并保存为TEXT/VARCHAR.检索同一列时,将String转换为JSONObject.
例如
写入DB
String stringToBeInserted = jsonObject.toString();
//and insert this string into DB
Run Code Online (Sandbox Code Playgroud)
从DB读取
String json = Read_column_value_logic_here
JSONObject jsonObject = new JSONObject(json);
Run Code Online (Sandbox Code Playgroud)
Ois*_*sin 28
另一种方法是使用SQLite的新JSON扩展.我自己只是遇到过这个问题:https://www.sqlite.org/json1.html这将允许您执行一定级别的查询存储的JSON.如果您使用VARCHAR或TEXT存储JSON字符串,则无法查询它.这是一篇很棒的文章,展示了它的用法(在python中)http://charlesleifer.com/blog/using-the-sqlite-json1-and-fts5-extensions-with-python/
| 归档时间: |
|
| 查看次数: |
101239 次 |
| 最近记录: |