I want to store the directories (present on the disk) into a database, maintaining their hierarchical/tree structure.
Here’s a figure:
(ROOT)
/ \
Dir2 Dir3
/ \ \
Dir4 Dir5 Dir6
/
Dir7
I am using the SQLite database.
Please suggest me:
-
the SQL query to store above structure in SQLite database, and
-
a query to retrieve full path of the directory when I select one.
i.e. suppose I select
Dir7then I should get the full path likeROOT/Dir2/Dir4/Dir7
Here’s a quick closure table example for SQLite. I’ve not included the statements for inserting items into an existing tree. Instead, I’ve just created the statements manually. You can find the insert and delete statements in the Models for hierarchical data slides.
For the sake of my sanity when inserting the IDs for the directories, I renamed the directories to match their IDs:
Create tables
Insert directories into
filesystemtableCreate the closure table paths
Run some queries