使用txt文件存储数据
这个问题可能看起来很奇怪,但我已经寻找答案很长时间了,但找不到任何答案。
假设您有一个博客,并且该博客像任何其他博客一样有许多帖子条目。现在每个帖子都可以有简单的用户评论。没有类似的按钮或任何其他需要数据管理的资源。现在的问题是:我可以将用户评论存储在单个文本文件中吗?每个帖子都将与一个包含评论的文本文件相关联。因此,如果我有 n 个帖子,我就会有 n 个文本文件。
我知道我可以完美地做到这一点,但我从未在其他地方见过它,也没有人在谈论它。对我来说,这似乎比将所有帖子中的所有评论存储在单个 mysql 表中更好,但我不知道是什么让它如此糟糕以至于还没有人实现它。
This question might seem strange but I have been searching for an answer for a long time and I couldn't find any.
Let's suppose you have a blog and this blog has many post entries just like any other blog. Now each post can have simple user comments. No like buttons or any other resource that would require data management. Now the query is: Can I store user comments on a single text file? Each post will be associated to a text file that holds the comments. So, if I have n posts I'll have n text files.
I know I can perfectly do this, but I have never seen it anywhere else and no one is talking about it. For me this seems better than storing all coments from all posts in a single mysql table but I don't know what makes it so bad that no one has implemented it yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将评论存储在与相应帖子关联的文本文件中?以免看看这是不是个好主意。
好的,添加新注释很容易 - 将新文本写入文件。但是数据的格式呢? CSV?好的,那么您必须在渲染之前对其进行解析。
分页。如果您有很多评论,您可以考虑为其创建分页导航。当然,这很容易做到。但您需要打开该文件并读取所有记录才能提取 20 条记录。
批准您的评论。有人发表了新评论。您将其置于待处理状态。所以..在管理面板中,您需要找到那些标记的评论并进行相应的处理 - 保存或删除。您认为文本文件方便吗?如果用户决定自己删除其评论,则相同。
如果您有很多评论和很多帖子,则读取文件会比使用数据库时慢。
可扩展性。有一天,您决定扩展您的评论功能,让一条评论能够回复另一条评论。您将如何处理文本文件?或者以 nico 的评论为例:“6 个月后,您将希望在评论中添加评级字段。 ..你会很头疼。或者,只需运行一个简单的 ALTER 查询”。
这只是为了开始。有人可能会添加一些东西。
Storing comments in text files associated with corresponding post? Lest see if it's good idea.
Okay adding new comments easy - write new text to the file. But what about format of your data? CSV? Ok then you would have to parse it before rendering.
Paging. If you have a lot of comments you may consider creating paging navigation for it. It can be done easily, sure. But you would need to open the file and read all the records to extract say 20.
Approve your comments. Someone posted new comment. You place it with pending status. So.. In admin panel you need to find those marked comments and process then accordingly - save or remove. Do you think it's convinient with text files? The same if use decided to remove its comment himself.
Reading files if you have many comments and many posts will be slower the it would be in case of database.
Scalability. One day you deside to extend you comments functionality to let one comment to respond to another. How would you do it with text files? Or example from comments by nico: "In 6 months time, when you will want to add a rating field to the comments... you'll have a big headache. Or, just run a simple ALTER query".
This is just for beggining. Someone may add something.
嗯,有充分的理由解释为什么不这样做。我不可能一一列举它们,但首先想到的是:
数据库比纯文本文件更高效、更灵活。您可以为各个评论建立索引、搜索和分配键,并根据其键编辑和删除任何评论。
此外,如果博客很大,您将获得大量文本文件。虽然这本身并不是问题,但如果将它们全部保存在一个目录中,它可能会变得不成比例,并确实增加查找和打开特定文本文件所需的访问时间。
Well, there are good reasons why this isn't done. I can't possibly name them all, but the first things that come to mind:
Databases are much more efficient and flexible than plain text files. You can index, search and assign keys to individual comments and edit and delete any comments based on their key.
Furthermore, you'd get a huge pile of text files if the blog is quite big. While in itself that's not a problem, if you all save them in one directory, it can grow out of proportion and really increase the access time needed to find and open a specific text file.