简体   繁体   中英

How to build a searchable database?

I would like to create (or use, if one already exists) a command-line based app that creates, modifies and searches a database.

This database would ideally be a simple text file where each line is an entry.

As in this simple example:

Apple    Fruit    Malus              Green/Red      55
Banana   Fruit    Musa acuminata     Yellow         68
Carrot   Veget.   D. carota          Orange         35

Let's say this text is stored in ~/database.txt

I'd like to be able to search for all entries that are of the type fruit (returning, Apple and Banana ) or all entries that have kilocalories that are less than 60 (returning Apple and Carrot ) on the command line.

The returns should happen through standard terminal output and should look like this:

$mydatabasesearch cal '<60'

Apple    Fruit    Malus              Green/Red      55
Carrot   Veget.   D. carota          Orange         35

Also, being able to add to the database through the command line would be awesome!

Is there anything around that does this? If not, how would you recommend I write such an app? I know a bit of C++ but that's it...

Take a look at sqlite . It is a bit more complex than plain text files, but a lot more powerful.

Plain text files are not really considered databases.

If you want to stick to textfiles and the commandline, have a look at the usual unix utilities like grep , awk , and the coreutils package ( cat , cut , uniq ,...) which work on plaintext files. Add those commands to a shellscript and you're done.

As soon as you move to some sort of database system you won't have textfiles as storage anymore.

Aside the already mentioned sqlite, the Berkeley DB library might also be worth a look if you want to write your own program. Both libraries should be good in your case since they don't require an external database server (like mysql)

You can do this in Unix anyway with a delimited file SED and maybe some simple commandline perl which you can wrap in a bash script. There is a good tutorial for the data manipulation at wikibooks and the SED you could use is probably all here and in the part two tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM