How to save a Json String in sqlite? - android

I have an sqlite database. The column where I want to save my JsonString in, has the data type TEXT. Everytime I want to insert a dataset I get no error until I try to read it out again, then I get an error that my curser may be at the wrong place. But when instead of a json give a usual string everything works fine. Since I don't really now what symbols might be in my json, is there at all a way to put a json into an sqlite database? (It is a large json so I can't really insert every value of it seperatly).
How can I save a JsonString else so that I can get I even if my application is closed?
I know I could put it in al file but since a database has a better structure and basiccly is a file too I would prefer to find a way working with the database.

Save as a string:
json.toString();
Although I would parse it, it is faster this way.

Related

Is this good idea to save data like json in Preference?

In my android app I load some data (JSON) from wed server. And I need to store that data.
All data, which I want to save - it is fixed number (for ex. 5) of JSONObjects with 30-50 fields.
Is this good idea to save each JSONObject in Preference? Or will be better to use SQLite?
note: why I ask?
save in Preference - implementation take 10 minutes.
use SQLite - implementation take one or two days, and required to
edit database, when I will add new fields.
I'd prefer database, and if you need 2 whole days for a reasonably simple database setup, you should perhaps take any chance to practice with it.
However, if you're sure you're never going to have any advantage of (complexer) queries, then saving it into a database might not give you any real advantages.
I would prefer sharedpreference here , because size of data is not too big. If size of data was too big and had many fields then database would be first choice. You can save the whole json String into preference and use it after parsing at run time.
My implementation choice would be a database table that stores the object as something you can then transform into a native intermediate object. By keeping the local data stored as JSON you're potentially locking yourself into keeping a JSON parser within the app for the foreseeable future even if your web service changes its format.
For example, if your web service changes to XML you're still going to have to keep the JSON parser in the app to handle those users who upgrade the app and have to do a data migration.
You could save JSON directly in SQLite database. If you parse data and then save in database then it would take some time for you to make everything work. But think about saving JSON directly in database. It will require two fields only in database link and json response.
Advantages:
- Big time saving
- You could save the same solution in your future apps for saving JSON in database.
Disadvantages
- You will not be able to query data
- You will have to parse every-time

Where to store json datas instead of using sqlite in android?

I am accessing the datas from json, storing the datas from json in sqlite and then retrieving it and display them on screen. It takes too long time for this process. Which is the better way to reduce the time consumption instead of storing and retrieving the datas from sqlite? Can anyone help me in this issue?
You can store the JSON as plain text in a file.
A nice place to put them would be your application's cache directory.
https://developer.android.com/reference/android/content/Context.html#getExternalCacheDir()
https://developer.android.com/reference/android/content/Context.html#getCacheDir()
You can store a JSON Data in a File or in a Shared Preference. Check Saving data in android training
I also use the file system to store json. It can be easier for debugging as you can have the json.tostrong methods indent the json yo make it easier to read using the android file browser.
i make a filesystem class that accepts and returns json so apps just see the file system as a json store.

Json parsed data need to store in sqlite database in android

I have parsed URL and I got the JSON objects,string values I need to store this into database.
My Question is:
which is the best way to insert these JSON Objects?
I am storing each string value.
Is there any other method which is useful?
You could store the entire JSON in a big text field, but if you ever want to access the values individually, you need to store them in individual columns.

Storing Json parsed in Android using GSON

I'm very new to Android and I'm currently working on Android app that will parse JSON from a Restful API and display some of the data in a list view. I've looked into using GSON for parsing as the JSON was quite complex. Now my main problem is figuring out how to store the data somewhere so the app doesn't need to reload itself every time the activity is clicked on. I've looked at few questions here but they all seem to point to JSON only not GSON. Could anyone recommend an efficient way of doing this?
Thanks
if you want to store the data permanent it's the best way to insert your deserialized objects into a sqlite database or store them local to the disk as a file.
if your intention is to load the data for each startup, it's easier. just put your object into a public static object. so you can check if the object is not null or not. if it's null you have to load the data.
another opportunity is using the SharedPreferences.

How to improve performance: database or text file?

My Android app is a client that fetchs datas from a Web Service and show them to the user. I would like to know how to improve performance and avoid continuos connections.
Everytime I click the botton "show" for example I make the connection and get the json string. I know that my string will not change during the day (hopefully).
It's better to get the json string and save it in a text file, save the string on a DB (with "date" and "string" columns for example) and then deserialize it, or directly deserialize the string and save as proper objects in the DB?
Thanks a lot!
If it's a little string, say under 1K, put it in Shared Preferences. Otherwise, if your data is going to remain relevant for a few days, use a database. If your data is going to be pretty much thrown out and refreshed completely each day, and is a few KB, write it in a cache file.

Categories

Resources