如何在 Python 执行之间保留数据
我正在用 Python 开发一个个人项目,我需要某种形式的持久数据。数据可容纳 2-3 个表,每个表有 10-20 列和 100-200 条记录。我对 SQL 有基本的了解,所以数据库似乎有意义。
我是Python新手,所以我不熟悉Python的数据库接口选项。我也听说过酸洗,但不确定这是否是适合我的项目规模的更好的解决方案。有人可以推荐一个好的解决方案吗?
I am working on a personal project in Python where I need some form of persistent data. The data would fit in 2-3 tables of 10-20 columns and 100-200 records each. I have a basic understanding of SQL, so a database seems to make some sense.
I am new to Python, so I am not familiar with the options for database interface from Python. I have also heard about pickling and am not sure if that would be a better solution for my project size. Can anyone recommend a good solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
或者,如果您只想在执行之间保留数据 - 对于如此小的数据集,您可以查看 pickle 模块用于持久化,并且在执行期间将数据加载到内存中。
这是一个简单的解决方案 - 但对于个人项目来说可能就足够了。
Or, if you just want to persist data between executions - for such a small data set you could have a look at the pickle module for persistency, and just load the data into memory during execution.
It's a simple solution - but for a personal project it might be enough.
为此,您应该使用 sqlite3 模块,它包含在 Python 中。
另外,您可能还想寻找 ORM 解决方案。
You should use sqlite3 module for this, it is included in Python.
Also you may want too look for an ORM solution.
这听起来数据很少。 SQL DB 可能有点过大,尤其是在 ORM 之上的情况下。我会检查 JSON 是否可以完成这项工作......
This sounds like very few data. An SQL DB might be overkill, especially with an ORM on top. I'd check whether JSON could do the job...
我同意使用sqlite3。它非常易于使用,您无需担心必须设置数据库服务器。您还应该查看 SQLAlchemy 库。
I agree with using sqlite3. It is very easy to use, you don't need to worry about having to set up a database server. You should check out the SQLAlchemy library too.
真正的问题是您想对数据执行什么样的操作。
就存储可能性而言,最简单的解决方案确实是 sqlite3 和 pickle。
您选择的解决方案基本上取决于使用 SQL 还是 Python 是否是您管理数据的最简单方法。 SQL 在复杂操作方面可能比 Python 更好,但 Python 绝对更轻量级、更简单,因此对于简单操作来说是一个不错的选择。所以,如果使用pickle+Python太麻烦的话,那么sqlite3是一个非常好的选择。
The real question is really what kind of operations you want to do with your data.
As far as storage possibilities, the simplest solutions are indeed sqlite3 and pickle.
The solution that you will choose depends basically on whether using SQL or Python is the easiest way for you to manage your data. SQL is probably better at complex operations than Python, but Python is definitely more lightweight and simpler, and therefore is a good choice for simple operations. So, if using pickle+Python is too cumbersome, then sqlite3 is a very good choice.
Peewee 是另一个与 SQLite 配合使用的 ORM。它是 SQLAlchemy 的替代方案。如果使用 SQLite,我会考虑将 Peewee 用于宠物项目,将 SQLAlchemy 用于专业工作。我通常不会直接使用 SQLite。
Peewee is another ORM that works with SQLite. It is an alternative to SQLAlchemy. If using SQLite, I would consider Peewee for pet projects and SQLAlchemy for professional work. I typically would not use SQLite directly.