python SQLAlchemy 在 MySQLdb 上的目的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2550292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:54:43  来源:igfitidea点击:

Purpose of SQLAlchemy over MySQLdb

pythonsqlmysqlsqlalchemy

提问by Eric Pruitt

Why do people use SQLAlchemy instead of MySQLdb? What advantages does it offer?

为什么人们使用 SQLAlchemy 而不是 MySQLdb?它有什么优势?

回答by Mike Graham

You don't use SQLAlchemy instead of MySQLdb—you use SQLAlchemy to access something like MySQLdb, oursql (another MySQL driver that I hear is nicer and has better performance), the sqlite3 module, psycopg2, or whatever other database driver you are using.

您不使用 SQLAlchemy 代替 MySQLdb — 您使用 SQLAlchemy 来访问诸如 MySQLdb、oursql(我听说另一个更好且性能更好的 MySQL 驱动程序)、sqlite3 模块、psycopg2 或您正在使用的任何其他数据库驱动程序之类的东西。

An ORM (like SQLAlchemy) helps abstract away the details of the database you are using. This allows you to keep from the miry details of the database system you're using, avoiding the possibility of errors some times (and introducing the possibility of others), and making porting trivial (at least in theory).

ORM(如 SQLAlchemy)有助于抽象出您正在使用的数据库的详细信息。这使您可以避开正在使用的数据库系统的肮脏细节,避免有时出现错误的可能性(并引入其他人的可能性),并使移植变得微不足道(至少在理论上)。

回答by Alex Martelli

Easier portability among different DB engines (say that tomorrow you decide you want to move to sqlite, or PostgreSQL, or...), and higher level of abstraction (and thus potentially higher productivity).

在不同的数据库引擎之间更容易移植(假设明天你决定要迁移到 sqlite、PostgreSQL 或...),以及更高级别的抽象(从而可能提高生产力)。

Those are some of the goodreasons. There are also some bad reasons for using an ORM, such as not wanting to learn SQL, but I suspect SQLAlchemy in particular is not really favored by people for such bad reasons for wanting an ORM rather than bare SQL;-).

这些是一些很好的理由。使用 ORM 也有一些不好的理由,例如不想学习 SQL,但我怀疑 SQLAlchemy 特别是因为想要 ORM 而不是裸 SQL 的这种不好的理由并没有真正受到人们的青睐;-)。

回答by wescpy

In addition to what Alex said...

除了亚历克斯所说的......

  1. "not wanting to learn SQL" is probably a bad thing, however, if you want to get more non-technical people involved as part of the development process, ORMs do a pretty good job at it because it does push this level of complexity down a level. One of the elements that has made Django successful is its ability to let "newspaper journalists" maintain a website, rather than software engineers.

  2. one of the limitations of ORMs is that they are not as scalable as using raw SQL. at a previous job, we wanted to get rid of a lot of manual SQL generation and switched to an ORM for ease-of-use (SQLAlchemy, Elixir, etc.), but months later, I ended up having to write raw SQL again to get around the inefficient or high latency queries that were generated by the ORM system.

  1. “不想学习 SQL”可能是一件坏事,然而,如果你想让更多的非技术人员参与到开发过程中,ORM 在这方面做得非常好,因为它确实将这种复杂程度降低了一个等级。使 Django 成功的要素之一是它能够让“报纸记者”而不是软件工程师维护网站。

  2. ORM 的局限性之一是它们不像使用原始 SQL 那样具有可扩展性。在之前的工作中,我们想摆脱大量手动 SQL 生成并切换到 ORM 以方便使用(SQLAlchemy、Elixir 等),但几个月后,我最终不得不再次编写原始 SQL绕过 ORM 系统生成的低效或高延迟查询。