How to store json data in sqlite

白昼怎懂夜的黑 提交于 2020-01-21 12:13:06

问题


Im hard pressed to store data as 'JSON' format into my sqlite database for an rails application. I have searched for how to store data as JSON in my sqlite database but am not seeing many alternatives which are promising. Any one who can guide me on how this can be done?


回答1:


You need to generate a string from your JSON and then save that string in your database as a regular string.

require 'json'
my_hash = {:hello => "goodbye"}
puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"

When you need to use that JSON object, you select your json string and convert it to JSON object using:

json_object = JSON.parse(string)

You can read about JSON objects here: http://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html




回答2:


Tried first using 'JSON' as an data type but since SQLite had no support for 'JSON' data type it failed. Then again I performed an migration with the data type for the attribute set up as type 'string' and it started to work.



来源:https://stackoverflow.com/questions/37065966/how-to-store-json-data-in-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!