SQLAlchemy 还是 psycopg2?

发布于 2024-12-22 04:32:16 字数 611 浏览 0 评论 0原文

我正在编写一个快速而肮脏的脚本,需要与数据库(PG)交互。

该脚本是对现有问题的务实、战术解决方案。然而,我预计该脚本将随着时间的推移演变成一个更加“精致”的系统。鉴于目前它的组装速度非常快(即我没有时间翻阅大量文档),我很想走快速而肮脏的路线,使用 psycopg。

psycopg2 的优点(据我目前的理解)是:

  1. 用 C 编写,比 sqlAlchemy(用 Python 编写)快?
  2. DBAPI 上没有抽象层,因为仅适用于一个数据库和一个数据库(意味着 -> 快速)
  3. (目前),我不需要 ORM,因此我可以直接执行 SQL 语句,而无需学习新的 ORM语法(即轻量级)

缺点:

  1. 我知道我会想要一个 ORM 进一步沿着
  2. psycopg2 的路线(“过时”?) - 不知道它会保留多久

我的看法是SqlAlchemy(缓慢/解释、臃肿、陡峭的学习曲线)真实 - 无论如何我可以以“粗略且准备好”的方式使用 sqlAlchemy 我想使用 psycopg - 即:

  1. 直接执行 SQL 语句,而不必弄乱 ORM 层等等。

有这样做的例子吗?

I am writing a quick and dirty script which requires interaction with a database (PG).

The script is a pragmatic, tactical solution to an existing problem. however, I envisage that the script will evolve over time into a more "refined" system. Given the fact that it is currently being put together very quickly (i.e. I don't have the time to pour over huge reams of documentation), I am tempted to go the quick and dirty route, using psycopg.

The advantages for psycopg2 (as I currently understand it) is that:

  1. written in C, so faster than sqlAlchemy (written in Python)?
  2. No abstraction layer over the DBAPI since works with one db and one db only (implication -> fast)
  3. (For now), I don't need an ORM, so I can directly execute my SQL statements without having to learn a new ORM syntax (i.e. lightweight)

Disadvantages:

  1. I KNOW that I will want an ORM further down the line
  2. psycopg2 is ("dated"?) - don't know how long it will remain around for

Are my perceptions of SqlAlchemy (slow/interpreted, bloated, steep learning curve) true - IS there anyway I can use sqlAlchemy in the "rough and ready" way I want to use psycopg - namely:

  1. execute SQL statements directly without having to mess about with the ORM layer, etc.

Any examples of doing this available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蝶舞 2024-12-29 04:32:16

SQLAlchemy 是一个 ORM,psycopg2 是一个数据库驱动程序。这是完全不同的事情:SQLAlchemy 生成 SQL 语句,而 psycopg2 将 SQL 语句发送到数据库。 SQLAlchemy依赖psycopg2或其他数据库驱动程序与数据库通信!

作为一个相当复杂的软件层,SQLAlchemy 确实增加了一些开销,但它也极大地提高了开发速度,至少在您学习了该库之后是这样。 SQLAlchemy 是一个优秀的库,将教您整个 ORM 概念,但如果您不想一开始就生成 SQL 语句,那么您就不需要 SQLAlchemy。

SQLAlchemy is a ORM, psycopg2 is a database driver. These are completely different things: SQLAlchemy generates SQL statements and psycopg2 sends SQL statements to the database. SQLAlchemy depends on psycopg2 or other database drivers to communicate with the database!

As a rather complex software layer SQLAlchemy does add some overhead but it also is a huge boost to development speed, at least once you learned the library. SQLAlchemy is an excellent library and will teach you the whole ORM concept, but if you don't want to generate SQL statements to begin with then you don't want SQLAlchemy.

倚栏听风 2024-12-29 04:32:16

要与数据库对话,任何人都需要驱动程序。如果您使用像 Oracle 的 SQL Plus、Mysql 的 MysqlCLI 这样的客户端,那么它将直接运行查询,并且该客户端附带 DBServer 包。

为了从外部与 java、c、python、C# 等任何语言进行通信,我们需要该数据库的驱动程序。 psycopg2 是从 python 运行 PostgreSQL 查询的驱动程序。

SQLAlchemy 是 ORM,与数据库驱动程序不同。它将为您提供灵活性,因此您可以在没有任何数据库特定标准的情况下编写代码。 ORM 为程序员提供了数据库独立性。如果你在ORM中编写object.save,那么它会检查该对象与哪个数据库关联,并根据后端数据库生成插入查询。

To talk with database any one need driver for that. If you are using client like SQL Plus for oracle, MysqlCLI for Mysql then it will direct run the query and that client come with DBServer pack.

To communicate from outside with any language like java, c, python, C#... We need driver to for that database. psycopg2 is driver to run query for PostgreSQL from python.

SQLAlchemy is the ORM which is not same as database driver. It will give you flexibility so you can write your code without any database specific standard. ORM provide database independence for programmer. If you write object.save in ORM then it will check, which database is associated with that object and it will generate insert query according to the backend database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文