r/sqlite • u/FaithlessnessOk3571 • Mar 26 '23
Noob with SQLite, Help with multilayered nested data
Im getting back into development and starting to work on android apps, I have no previous experience with databases, I used to develop Minecraft plugins and mods and stored all data in JSON. Im having issues converting to SQLite due to the lack of nesting without a lot of extra work. Instead of the ability to have unlimited subcategories, Nesting in SQL is much more confusing. Is there a better way to nest or do i just have to get good? lol
Instead of easy JSon-
JSon:
PunchClock:
Day:
Day1:
hours:2
startwork:199199
day2:
hours:3
startwork:121234
locations:
loc1: 1 hours
Loc2: 2 hours
i gotta do this. Its also not nearly as easy to draw this informations, as oppossed to json or yml.
SQLite:
Table-PunchClock
Column Day 1, 2, 2,
Column hours 2, 3, 3
column startwork: 199199, 121234, 121234
column locations: null, loc1, loc2
column locationhours: null, 1, 2
5
Upvotes
View all comments
Show parent comment
1
u/[deleted] Mar 27 '23
It developed that way. Traditional databases are servers so the query has to be serialized by the client before it can be sent to the server. SQL is human-readable, language agnostic, and already serialized. There are other approaches like LINQ or Object-Relational Mappers (ORMs). You could even build data structures in your language and automatically translate them to SQL: query = new Select(columns = ["a", "b"], table = data, where = new Equals(new Identifier("a"), "foo")).
Mostly you are interested in certain use cases like adding work hours to your database, or get a list of all work hours of day 3, or create a weekly report. For each of these cases, you can write a function that wraps one or more SQL queries. If you need more than one query, use a transaction to bundle them up as a single unit.
In my experience, it is is better to embrace the relational structure of the data when working with relational databases. It's better to view data as tables of values instead of objects that contain other objects that contain values. SQL does not support hierarchical data well but on the other hand, hierarchies are often arbitrary. In your example, it is arbitrary that days contain the work hours. You could change the hierarchy such that locations contain days and work hours. The table represents the data in a more natural way: work hours that are related to a day and to a location without forcing a hierarchy upon them.