JSON or Sqlite?

Asked

Viewed 846 times

3

I’m developing a simple app on PhoneGap and I will need to have basically 2 tables. One to store the names of the series and another to store the exercises of each specific series. But I’m not sure how the JSON like the SQLite, I wonder if for such a simple information, which would be better to use.

  • It is difficult to compare the two, because one is a file format and the other is a database... What is your main difficulty? Or your main goal? (type: ease to generate/interpret data, ease to save/change/delete on disk, etc.) Have experience with SQL?

  • 1

    My difficulty is to know how I will be able to treat this data in a simpler way, since I will have to use JS for this, I already have a certain experience with SQL yes, but I have never done anything using JS. My biggest problem is how to handle this data.

  • 1

    I’ll let those with Phonegap experience answer, then. Because in fact, it is very easy to handle JSON data in Javascript, but you would have to do the persistence of it in hand. On the other hand, it is very easy to make persistence via SQL, but you would have to handle the data at hand...

1 answer

5


The main difference of these two approaches is that if you are going to use JSON (and write to a local file using Phonegap’s I/O Apis), you are responsible for deciding how to store / read this information in the local file system - and depending on how you record the data the way you access them will vary quite.

Typically, using JSON to store data works well when you can write and read all data at once (*) - you serialize (e. g., JSON.stringify) the object (or array) that contains your data, and records everything in a local file. When you need to access the data, you read the local file and load all the data into memory. If you want to access only part of the data, still (in this example) you would have to load all the data into memory. To insert / update / remove a row from the table (e.g., a series of exercises), you would add / modify / remove it from the array in memory and again save the entire file.

If you use a database such as Sqlite, you have more control over what will be accessed on disk. You can read only a part of the table, edit / remove only one row from the table.

The best solution is usually the simplest that works for your scenario. If the amount of information that will be stored in the client is small enough (and the definition of "small enough" depends on factors such as amount of data, memory of the devices where you imagine your application will run, etc.), which can always be loaded in memory without problems, so using the file system directly via the JSON-fied tables is a good solution. If the cost of having all objects in memory is too high, or if you need other Features of a database (e.g., indices for search performance, simplicity in performing JOIN's, etc.), so using a BD (such as Sqlite) is more advantageous.

(*) Your case seems to be simpler than the general case - you have two tables (1:n ratio, I understand), so you can lessen some of the limitations of the file system option. For example, you might have a file (which would be read in memory) with the name of the series, and one of the fields of the object stored in that file would be the name of another file that contains the exercises for that series, and some other field by which you want to search (e.g., date of series). Only when the application needs to access the data from specific series would you need to read them into memory, and you can also edit / add / remove specific series without always having to write / load your entire "database".

  • Carlos, very good your explanation. Clarified all my doubts regarding this. Thank you very much!

  • Some people would say that the simplest is Sqlite in any situation to avoid you having to deal with everything that is needed to ensure the proper functioning of the file flat. Most programmers don’t even know everything that can go wrong. And if the programmer has experience with SQL, it can be even easier. Doing something different than what you’re used to isn’t usually the simplest solution, even when it’s simple on its own. I am not disagreeing with the answer, just putting an additional point to consider.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.