使用独立的 C++ 存储数据应用
我使用 Apache、PHP 和 MySQL 进行 Web 开发和本地应用程序。在过去的几年里,我一直在慢慢学习 C++,并想在今年夏天构建一个应用程序。具体来说,我想制作一个“图书馆”应用程序,在其中可以存储有关我拥有的书籍、CD 和记录的信息。我知道存在这种类型的应用程序,但我想学习 C++,这似乎是一个很好的方法。
这里有几个问题:
是否可以创建一个不需要数据库来存储数据的独立应用程序?
如果上述问题 1 的答案是“是”,那么对于可能需要管理大量数据的应用程序来说,这样做是个好主意吗?
您建议将哪些数据存储选项与 C++ 应用程序一起使用?
谢谢!
更新 嗯,对此有很多好的答案。这是一个非常棒的网站,有很多贡献者。事实证明,我现在可能真的不需要走 C++ 路线。我现在意识到,我最感兴趣的是编写一个可以充当“图书馆”组织系统的应用程序,比我对 C++ 更感兴趣。感谢大家的回答!
I work with Apache, PHP, and MySQL for web development and local applications. For the past couple of years I have slowly been learning C++ and want to build an application this summer. Specifically, I want to make a "library" application in which I can store information about the books, CDs, and records that I own. I know this type of app exists, but I want to learn C++ and this seems like a good way to go about it.
Here are a few questions:
Is it possible to create a stand-alone application that does not require a database for storing data?
If the answer to #1 above is "yes", is it a good idea to do this for an application that could potentially need to manage a lot of data?
What data-storage options would you recommend for use with a C++ application?
Thanks!
Update
Well, there were a lot of good answers to this. This is such a great site with so many contributors. It turns out that I might not really need to go the C++ route for now. I now realize that I am most interested in writing an application that can function as a "library" organizational system more than I want to pursue C++. Thanks to everybody for your answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,您可以使用某种自定义文件格式来存储数据。
这并不是一个好主意,除非您真的想了解如何在结构化文件中存储数据。
我会看看 SQLite。它是一个数据库,但它不需要单独的引擎。
Yes, you could do some sort of custom file format for storing data.
It's not really a good idea, unless you really want to learn about storing data in a structured file.
I'd look at SQLite. It is a database, but it doesn't need a separate engine.
如果您不想使用数据库,您可能希望将数据保存到一个文件(或多个文件)中。如果是这样,问题可能是:哪种文件格式最好?答案取决于多种因素:
1)访问速度快、体积小且易于解析?答案是“二进制数据”。只需使用 fwrite 将数据直接按原样写入输出文件。有两个缺点:这些文件不是人类可读的,并且维护不同的版本很麻烦。如果您读取的数据不完全符合您的预期,您很快就会遇到麻烦。
2)人类可读且易于维护?这就是 XML 的用途。有现成的解析器,如tinyXML,它们是将数据写入文件的优秀工具。缺点是编写加载/保存例程需要更多时间(很多情况下)。
3) 为您完成这项工作的图书馆。 MFC 提供了 CArchive 类,但还有其他可能更好的工具可以执行此操作。
If you don't want to use a database, you might want to save your data to a file (or maybe more than one). If so, the question is probably: which file format is the best? The answer depends on various factors:
1) Fast access, small size and easy to parse? The answer is "binary data". Just write your data directly as-is to the output file using fwrite. There are two downsides: the files are not human readable and it's cumbersome to maintain different versions. If the data you read isn't exactly what you expect, you run quickly into trouble.
2) Human-readable and easy to maintain? That's what XML is for. There are ready to use parsers like tinyXML which are excellent tools for writing data to file. The downside is that writing loading/saving routines takes more time (a lot is many cases).
3) Libraries that do the job for you. The MFC provides the CArchive class, but there are other and probably better tools to do this.
为了学习,不要使用数据库引擎会对你有很大帮助,除非你想学习 SQL 和关系数据库设计。
另一方面,使用数据库引擎将使开发更快、更容易,因为它会为不同的数据创建索引,以帮助您轻松高效地搜索。
所以这取决于你。
For the sake of learning, it'd help you a lot NOT to use a DB engine, unless you want to learn SQL and relational DB design.
On the other hand, using a DB engine is will make development faster and easier because because it'll create indices for different pieces of data to help you search easily and efficiently.
So it's up to you.
嗯……
当然,你可以这样做。
你所说的做法是回到 RDBMS 成功之前的时期。这并不难做到,但您可能会吸取一些经验教训:
这应该可以做到。
请注意,使用现代计算机硬件,您将管理的数据量不太可能成为性能或空间问题,因此不必担心节省几个字节 - 这不值得这么麻烦。
祝你好运,享受旅程。
Hmmm...
Of course, you can do this.
What you're talking about doing is stepping back in time to the period before the RDBMS became successful. This isn't hard to do but you might well take some lessons learned:
This ought to do it.
Note that the amount of data you will be managing, with modern computer hardware, is not likely to ever become a performance or space issue, so don't worry about saving a few bytes - it's not worth the hassle.
Good luck, and enjoy the ride.
鉴于您(显然)这样做主要是为了自学,而不是真正的使用,因此在没有数据库管理器来处理存储的情况下编写代码可能是有意义的。自己编写所有代码(稍微小心)通常会导致速度稍高,但代价是相当多的额外工作,并且通常会损失一些灵活性。从学习的角度来看,最大的问题是,您在此过程中学到的大部分内容仅适用于相当狭窄的情况(即,对于大多数典型的应用程序,您将想要使用数据库管理器,因此学会不做事很少能收获很多)。
一点也取决于您的预期用途/受众。如果您想一次支持多个用户,事情几乎立即就会变得困难得多(至少这样做是有效的)。如果您只对一个用户一次能够访问数据库感兴趣,那么事情就会变得简单得多。
Given that you're (apparently) doing this primarily for self-education, not real use, it might make sense to write the code without a database manager to handle the storage. Writing all the code on your own will (with a bit of care) typically result in slightly higher speed, at the expense of quite a bit of extra work and usually some loss of flexibility. The biggest problem from a learning viewpoint is that quite a bit of what you learn doing this will only apply under fairly narrow circumstances (i.e., for most typical applications, you will want to use a database manager, so learning to do without rarely gains much).
A bit also depends on your intended use/audience. If you want to support more than one user at a time, things get much more difficult almost immediately (at least doing so efficiently). If you're only interested in one user being able to access the database at a time, that keeps things a lot simpler.
我还推荐 SQLite。
从他们的网页上可以看出:
“SQLite 是一个软件库,它实现了独立、无服务器、零配置、事务性 SQL 数据库引擎。SQLite 是世界上部署最广泛的 SQL 数据库引擎。SQLite 的源代码位于公共领域。”
“不要将 SQLite 视为 Oracle 的替代品,而是 fopen() 的替代品”
I would also recommend SQLite.
From their web page:
"SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is in the public domain."
"Think of SQLite not as a replacement for Oracle but as a replacement for fopen()"