How to store JSON object in SQLite database

前端 未结 5 2023
夕颜
夕颜 2020-12-04 08:29

how do I store a JSON Object in an SQLite database? What is the correct way?

one place is the blob type column. if i can convert the JSON object into byte array an

相关标签:
5条回答
  • 2020-12-04 08:39

    https://github.com/requery/sqlite-android allows you to query JSON fields (and arrays in them, I've tried it and am using it). Before that I was just storing JSON strings into a TEXT column. It supports FTS3, FTS4, & JSON1

    As of July 2019, it still gets version bumps every now and then, so it isn't a dead project.

    • https://www.sqlite.org/json1.html (store and query JSON documents)
    • https://www.sqlite.org/fts3.html (perform full-text searches)
    0 讨论(0)
  • 2020-12-04 08:42

    Convert JSONObject into String and save as TEXT/ VARCHAR. While retrieving the same column convert the String into JSONObject.

    For example

    Write into DB

    String stringToBeInserted = jsonObject.toString();
    //and insert this string into DB
    

    Read from DB

    String json = Read_column_value_logic_here
    JSONObject jsonObject = new JSONObject(json);
    
    0 讨论(0)
  • 2020-12-04 08:44

    https://github.com/app-z/Json-to-SQLite

    At first generate Plain Old Java Objects from JSON http://www.jsonschema2pojo.org/

    Main method

    void createDb(String dbName, String tableName, List dataList, Field[] fields){ ...

    Fields name will create dynamically

    0 讨论(0)
  • 2020-12-04 08:50

    An alternative could be to use the new JSON extension for SQLite. I've only just come across this myself: https://www.sqlite.org/json1.html This would allow you to perform a certain level of querying the stored JSON. If you used VARCHAR or TEXT to store a JSON string you would have no ability to query it. This is a great article showing its usage (in python) http://charlesleifer.com/blog/using-the-sqlite-json1-and-fts5-extensions-with-python/

    0 讨论(0)
  • 2020-12-04 09:02

    There is no data types for that.. You need to store it as VARCHAR or TEXT only.. jsonObject.toString();

    0 讨论(0)
提交回复
热议问题