JSON or SQLite?

3

I'm developing a simple app in PhoneGap and I'm going to need to basically have 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 JSON and SQLite works, I'd like to know if for such simple information, which one would be best to use.

    
asked by anonymous 07.06.2014 / 04:15

1 answer

5

The main difference between these two approaches is that if you are going to use JSON (and write to a local file using the PhoneGap I / O APIs), you are responsible for deciding how to store / read this information in the file system local - and depending on how you write the data how to access them will vary greatly.

Typically, using JSON to store data works fine when you can write and read all data at once (*) - you serialize (eg, JSON.stringify ) the object (or array) which contains your data, and writes everything to 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, however (in this example) you would have to load all the data into memory. To insert / update / remove a table row (eg, a series of exercises), you would add / modify / remove it from the array in memory, and again write the whole file.

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

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

(*) Your case seems to be simpler than the general case - you have two tables (1: n ratio, as I understand it), so you can narrow down some of the limitations of the file system option. For example, you can 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 containing the exercises for that series, and some other field by which you want to do searches (eg, date series). Only when the application needs to access the specific series data do you need to read them for memory, and you can also edit / add / remove specific series without ever having to write / load your entire "database."

    
07.06.2014 / 15:40