在 python/flask 个项目中处理大型数据库的最佳方式
The best way to to handle large databases in python/flask projects
最近开始用flask,挺喜欢的。
过去我在 PHP 有一个系统,里面有很多数据库,比如市场营销、人力资源、财务等等。
每个数据库都有自己的 table,就像 HR 曾经有雇主、公司等等。
每个 tables 都是 PHP 中的一个 class,我们使用这个系统来促进 save/delete 因为它们在我们所有的系统中都被使用要做的是从 table/class 之一实例化一个新对象,其中哪一列是对象 属性,然后调用 $obj->Save() 在 table 中插入一个新行。
从那时起,编程已经发展了很多,所以我怀疑 python/flask 中是否有更有效的方法来做到这一点,而不是为每个 table 创建一个 class s 来自数据库,就像我以前在 PHP 中做的那样,我知道这是一个很大的问题,所以我很感激关于这个主题的书籍、wiki 等的推荐。
用高级编程语言与数据库交互的一种相当现代的方法是使用 ORM,即对象关系映射器。请参阅此 Stack Overflow thread 以获得很好的解释。
如果您使用的是 Flask,SQLAlchemy is the most popular choice, so much so that Flask actually has an extension called Flask-SQLAlchemy。请记住,您仍会将 classes 映射到数据库实体。然而,SQLAlchemy 的强大之处在于它在数据库之上提供了更高级别的抽象,这不仅仅是简单地将 class 映射到 table 行。根据文档:
SQLAlchemy considers the database to be a relational algebra engine, not just a collection of tables. Rows can be selected from not only tables but also joins and other select statements; any of these units can be composed into a larger structure. SQLAlchemy's expression language builds on this concept from its core.
这个 Stack Overflow thread 提供了更多 Python ORM 建议。
最近开始用flask,挺喜欢的。 过去我在 PHP 有一个系统,里面有很多数据库,比如市场营销、人力资源、财务等等。 每个数据库都有自己的 table,就像 HR 曾经有雇主、公司等等。
每个 tables 都是 PHP 中的一个 class,我们使用这个系统来促进 save/delete 因为它们在我们所有的系统中都被使用要做的是从 table/class 之一实例化一个新对象,其中哪一列是对象 属性,然后调用 $obj->Save() 在 table 中插入一个新行。
从那时起,编程已经发展了很多,所以我怀疑 python/flask 中是否有更有效的方法来做到这一点,而不是为每个 table 创建一个 class s 来自数据库,就像我以前在 PHP 中做的那样,我知道这是一个很大的问题,所以我很感激关于这个主题的书籍、wiki 等的推荐。
用高级编程语言与数据库交互的一种相当现代的方法是使用 ORM,即对象关系映射器。请参阅此 Stack Overflow thread 以获得很好的解释。
如果您使用的是 Flask,SQLAlchemy is the most popular choice, so much so that Flask actually has an extension called Flask-SQLAlchemy。请记住,您仍会将 classes 映射到数据库实体。然而,SQLAlchemy 的强大之处在于它在数据库之上提供了更高级别的抽象,这不仅仅是简单地将 class 映射到 table 行。根据文档:
SQLAlchemy considers the database to be a relational algebra engine, not just a collection of tables. Rows can be selected from not only tables but also joins and other select statements; any of these units can be composed into a larger structure. SQLAlchemy's expression language builds on this concept from its core.
这个 Stack Overflow thread 提供了更多 Python ORM 建议。