darkblackblue has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: perl create database with folder system
by hippo (Archbishop) on Dec 02, 2017 at 11:10 UTC | |
See Databases made easy in the Tutorials section. Once you've been through that and understand the basics you can move across to DBD::CSV which will allow you to work with CSV files in your native filesystem (with some restrictions of course). | [reply] |
by Anonymous Monk on Dec 03, 2017 at 10:37 UTC | |
| [reply] |
|
Re: perl create database with folder system
by erix (Prior) on Dec 02, 2017 at 11:38 UTC | |
In Standard-SQL database language, there is the concept of 'schema'. A schema is a grouping of tables inside a database. (This resembles the directory (=folder) of a filesystem enough that in my database I sometimes type 'mkdir' instead of 'create schema'). But your mention of cat.txt, makes me think you actually want to store text (from text files). That's possible but it has little to do with 'folders'. As always with database design, future queries should be specified, not so much (often vague) ideas about storage. Does cat.txt also contain table-like data? Or is it free-form text? | [reply] |
by Anonymous Monk on Dec 03, 2017 at 10:43 UTC | |
| [reply] |
by erix (Prior) on Dec 03, 2017 at 11:23 UTC | |
Here is a test program that I did with DBD::CSV (that was also hippo's plan). I've used /tmp as a 'folder'; you should of course eventually choose a better location. (It would kind of make sense if CREATE DATABASE did create a directory (=folder) but that wasn't implemented (fortunately). So, directory management has to be done separately.)
# Hope this helps... (I also made a version with postgres reading an underlying text-file, via postgres' file_fdw, but it depends on postgres and is read-only; it therefore seems less handy although the code is compact enough) update: Changed to use DBI instead of the explicit use DBD::CSV; added conditional INSERT. | [reply] [d/l] |
|
Re: perl create database with folder system
by NetWallah (Canon) on Dec 02, 2017 at 18:40 UTC | |
Here is some info to get you started with databases. CREATE and populate database (Assuming content above is saved as animal.sql: Query examples from the command line:
P.S. I have pointed you toward a traditional "relational" database. Followers of current technology are proponents of "nosql" databases, such as Elastic Search. All power corrupts, but we need electricity. | [reply] [d/l] [select] |
by afoken (Chancellor) on Dec 02, 2017 at 22:50 UTC | |
Just a small note about SQLite: It does not enfoce data types like other SQL databases do. This can be a little bit surprising. See FAQ 3: SQLite lets me insert a string into a database column of type integer! For client-server SQL databases, see also: Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-) | [reply] |
|
Re: perl create database with folder system
by Corion (Patriarch) on Dec 02, 2017 at 10:41 UTC | |
I think you will need to learn SQL for that. For accessing the database, DBI is the common way of doing that. If you are just starting out, DBD::SQLite is a good database to start with. | [reply] |
by Anonymous Monk on Dec 02, 2017 at 10:55 UTC | |
| [reply] |
|
Re: perl create database with folder system
by Marshall (Canon) on Dec 02, 2017 at 22:39 UTC | |
I think that some of the posts about SQL are a bit too "fancy" for a beginner. I would start with an idea of "how to represent the data in a single Excel spreadsheet".
I liked NetWallah's post at Re: perl create database with folder system. In creating the table, Animal, I let the DB assign a unique ID for each "row" (see code). This is normally a good idea- so good that you should explain why you don't want that. This idea guarantees and unique id for each row even with multiple simultaneous writers. There are some "do" statements, but the normal way using the Perl DBI is to "prepare" an SQL statement and then later execute it with some variables. There are a number of ways to get the results of "execute". I show one of those ways. The Perl DBI comes standard in many (if not all) distributions. I don't think that you will have to install anything to run the code below. I used to use a Firefox SQLite add-on as a UI but I am hearing that this is no longer supported. Bummer. Anyway, this code "runs": Update: I didn't add any indices to the Animal table. And I don't recommend that you do that until you understand more about the performance implications. Don't worry about this with a "small DB". I consider 1,000 - 10,000 records "small". In my testing with multi-million row SQLite DB's, it is possible to "shoot yourself in the foot" by over indexing in the pursuit of higher performance. This can confuse the query engine and actually slow things down. Anyway, what column to index and when to index that column is a complicated subject that I recommend you not deal with for your first DB's. | [reply] [d/l] |
|
Re: perl create database with folder system (Tree in SQL)
by LanX (Saint) on Dec 02, 2017 at 12:50 UTC | |
My first idea was that you want to implement a database just using the file system. But now I think you are asking about how to implement tree structures in an SQL table. This is speculation and we are primarily a Perl board, but table like this should do:
editFor instance the path structure /animal/cat/tiger/Roy is represented in the former table. There are different ways how to query this, like recursively or using self joins. You can also use a view based on self join if the tree's depth is fixed in order to visualize the path structure.
Cheers Rolf
| [reply] [d/l] [select] |
by choroba (Cardinal) on Dec 02, 2017 at 15:17 UTC | |
| [reply] [d/l] [select] |
by LanX (Saint) on Dec 02, 2017 at 15:36 UTC | |
If the max depth is fixed I can easily create a performant view where each column represents a level. SQL views on self joins are even updatetable in mysql.
editOf course there are attempts to use a path like strings as ID, ie 0_1 would be a parent of 0_1_15 , but this becomes a can of worms if you need to move branches ...
Cheers Rolf
°) integers to be more precise | [reply] [d/l] [select] |
|
Re: perl create database with folder system
by karlgoethebier (Abbot) on Dec 03, 2017 at 10:14 UTC | |
| [reply] [d/l] | |