简体   繁体   English

在 python/flask 项目中处理大型数据库的最佳方法

[英]The best way to to handle large databases in python/flask projects

Recently i started to use flask and I liked pretty much.最近我开始使用 flask,我非常喜欢。 In the past I had a system in PHP with a lot of databases like marketing, HR, finance and so on.过去我在 PHP 有一个系统,里面有很多数据库,比如市场营销、人力资源、财务等。 Each of this databases had their own tables like HR used to have employers, companies and so on.每个数据库都有自己的表格,例如 HR 曾经有雇主、公司等。

Each of this tables was a class in PHP, we used this system to facilitate save/delete since they were used all over the system all we had to do was instantiate a new object from one of the table/class where which column was a object property and then call $obj->Save() to insert a new row in the table. Each of this tables was a class in PHP, we used this system to facilitate save/delete since they were used all over the system all we had to do was instantiate a new object from one of the table/class where which column was a object属性,然后调用 $obj->Save() 在表中插入新行。

Programming has evolved so much since then so my doubt is if there's a more efficient way to do that in python/flask, instead of creating a class for each of the tables from the databases like I used to do in PHP, I know this is a large question so I would appreciate recommendations of books, wikis and so on about this topic.从那时起,编程已经发展了很多,所以我怀疑是否有更有效的方法在 python/flask 中做到这一点,而不是像我以前在 PHP 中那样为数据库中的每个表创建 class,我知道这是一个很大的问题,所以我会很感激有关该主题的书籍,维基等建议。

A fairly modern approach to interface with a database in high-level programming languages is to use an ORM, or Object Relational Mapper.在高级编程语言中与数据库交互的一种相当现代的方法是使用 ORM 或 Object 关系映射器。 See this Stack Overflow thread for a good explanation.请参阅此 Stack Overflow线程以获得很好的解释。

If you are using Flask, SQLAlchemy is the most popular choice, so much so that Flask actually has an extension called Flask-SQLAlchemy .如果您使用的是 Flask, SQLAlchemy是最受欢迎的选择,以至于 Flask 实际上有一个名为Flask-SQLAlchemy的扩展。 Keep in mind, that you will still be mapping classes to database entities.请记住,您仍然会将类映射到数据库实体。 However, the power of SQLAlchemy is that it provides a higher level of abstraction on top of the database, which can go beyond simply mapping a class to a table row.然而,SQLAlchemy 的强大之处在于它在数据库之上提供了更高级别的抽象,它可以 go 不仅仅是将 class 映射到表行。 According to the documentation:根据文档:

SQLAlchemy considers the database to be a relational algebra engine, not just a collection of tables. SQLAlchemy 将数据库视为关系代数引擎,而不仅仅是表的集合。 Rows can be selected from not only tables but also joins and other select statements;行不仅可以从表中选择,还可以从连接和其他 select 语句中选择; any of these units can be composed into a larger structure.这些单元中的任何一个都可以组成一个更大的结构。 SQLAlchemy's expression language builds on this concept from its core. SQLAlchemy 的表达式语言从其核心建立在这个概念之上。

This Stack Overflow thread provides more Python ORM suggestions.此 Stack Overflow 线程提供了更多 Python ORM 建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SQLAlchemy和Postgres在Flask中处理多个大量数据库之间切换的最佳方法 - Best way to handle switching between multiple, large amount of databases in Flask with SQLAlchemy and Postgres 处理Python中大型词典列表的最佳方法 - Best Way to handle Large List of Dictionaries in Python 在一个目录中处理多个 python 项目的最佳方法? - Best way to handle multiple python projects in one directory? 使用python flask上传大型csv文件的最佳方法 - Best way to upload large csv files using python flask 处理python(10万行)中的大量输入的最佳方法是什么? - What is the best way to handle large inputs in python (100 k lines)? 在 Flask 中处理 webhook 响应超时的最佳方法? - Best way to handle webhook response timeouts in Flask? 处理烧瓶POST单元测试的最佳方法 - Best way to handle flask POST unit test 用Flask + Python处理无效的GET / POST请求参数的最佳方法? - Best way to handle invalid GET/POST request parameters with Flask+Python? 在 Python 中没有 ORM 的情况下,在 http 服务器(Flask)中处理 sql 连接的最佳方法是什么? - What is the best way to handle sql connection in http server (Flask) without ORM in Python? 在 AWS Lambda 中实施 python flask 的最佳方法? - Best way to implement python flask in AWS Lambda?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM