SQLAlchemy 还是 psycopg2? [英] SQLAlchemy or psycopg2?

查看:46
本文介绍了SQLAlchemy 还是 psycopg2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

该脚本是对现有问题的务实、战术性解决方案.然而,我设想脚本会随着时间的推移演变成一个更精致"的系统.鉴于它目前正在快速组装(即我没有时间倾注大量文档),我很想使用 psycopg 走快速而肮脏的路线.

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.

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

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

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

缺点:

  1. 我知道以后我会想要一个 ORM
  2. psycopg2 是(过时"?) - 不知道它会保留多久

我对 SqlAlchemy 的看法(缓慢/解释、臃肿、陡峭的学习曲线)是否正确 - 无论如何我可以以粗略和准备好"的方式使用 sqlAlchemy,我想使用 psycopg - 即:

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. 直接执行 SQL 语句,无需处理 ORM 层等.

是否有这样做的可用示例?

Any examples of doing this available?

推荐答案

SQLAlchemy 是一个 ORM,psycopg2 是一个数据库驱动程序.它们是完全不同的东西:SQLAlchemy 生成 SQL 语句,而 psycopg2 将 SQL 语句发送到数据库.SQLAlchemy 依赖 psycopg2 或其他数据库驱动与数据库进行通信!

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!

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

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 a 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.

这篇关于SQLAlchemy 还是 psycopg2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆