如何建立一个可搜索的数据库?
我想创建(或使用,如果已经存在)一个基于命令行的应用程序,用于创建、修改和搜索数据库。
理想情况下,该数据库是一个简单的文本文件,其中每一行都是一个条目。
正如这个简单的示例所示:
Apple Fruit Malus Green/Red 55
Banana Fruit Musa acuminata Yellow 68
Carrot Veget. D. carota Orange 35
假设此文本存储在 ~/database.txt
中,
我希望能够搜索 fruit
类型的所有条目(返回 Apple
和 Banana
)或千卡小于 60
的所有条目(返回 Apple
和Carrot
) 在命令行上。
返回应该通过标准终端输出进行,并且应该如下所示:
$mydatabasesearch cal '<60'
Apple Fruit Malus Green/Red 55
Carrot Veget. D. carota Orange 35
另外,能够通过命令行添加到数据库将非常棒!
周围有什么东西可以做到这一点吗?如果没有,你会如何建议我编写这样的应用程序?我懂一点 C++
但仅此而已......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下 sqlite。它比纯文本文件稍微复杂一些,但功能更强大。
Take a look at sqlite. It is a bit more complex than plain text files, but a lot more powerful.
纯文本文件并不真正被视为数据库。
如果您想坚持使用文本文件和命令行,请查看常用的 UNIX 实用程序,例如
grep
、awk
和 coreutils 包 (cat、
cut
、uniq
、...),适用于纯文本文件。将这些命令添加到 shell 脚本中即可完成。一旦您迁移到某种数据库系统,您将不再使用文本文件作为存储。
除了已经提到的 sqlite 之外,如果您想编写,Berkeley DB 库也可能值得一看你自己的程序。这两个库都适合您的情况,因为它们不需要外部数据库服务器(如 mysql)
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)
无论如何,您都可以在 Unix 中使用分隔文件 SED 和一些简单的命令行 perl 来完成此操作,您可以将其包装在 bash 脚本中。 wikibooks 有一个很好的数据操作教程,您可以使用的 SED 可能全部是 < a href="http://www.catonmat.net/blog/sed-one-liners-explained-part-one/" rel="nofollow">此处 和第二部分教程。
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.