为什么要使用SQLAlchemy?它是非常有说服力的编码吗? [英] Why use SQLAlchemy? Is it very convinent for coding?

查看:1013
本文介绍了为什么要使用SQLAlchemy?它是非常有说服力的编码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于非常熟悉在Python中编写简单的SQL,我没有意识到使用SQLAlchemy或其他ORM可以获得的好处。

Being very familar with writing plain SQLs in Python, I havn't realized the benefits I would get by using SQLAlchemy or other ORMs.

谁不喜欢SQLs的程序?还是它使你的脚本穿越不同类型的数据库?

Is it designed for those who don't like SQLs in the programs? Or is it make your script crossing different types of database?

还是让你的脚本更加Pythonic?

Or it makes your script more Pythonic?

还是对于处理底层数据库,它为你自动执行了很多trival事件,所以你只需要写2-3行代码调用SQLAlchemy,它会为你完成所有的trival数据库操作?

Or is it very conveinent for some tasks dealing with underlying database, and it does a lot of trival things automatically for you, so you just write 2-3 lines of codes calling SQLAlchemy and it does all the trival database operations for you?

如果有人可以提供代码,使用纯SQL与使用SQLALchemy进行演示,并显示出对普通SQL的好处,这将是非常有用的。

It would be highly apppreciated if someone can provide codes demostrating using plain SQL vs using SQLALchemy and show the benefits over plain SQL.

谢谢!

推荐答案

你检查过吗?
https://groups.google.com/论坛/?fromgroups =#!主题/ sqlalchemy / VXXB12-3JCY

此外,Django ORM和SQL Alchemy之间的比较:

Also, a comparison between Django ORM and SQL Alchemy :

http:// blog。 mathieu-leplatre.info/sqlalchemy-a-brave-new-world.html

从上述Google群组连结贴上回应:

Pasting the response form the above google groups link:


Mark Erbaugh 22/11/2010

Mark Erbaugh 22/11/2010

我没有任何示例代码,我写了几个Python应用程序(和一个C ++应用程序)使用纯SQL,并且
开始使用SQLAlchemy在一个新的应用程序上工作,所以我会分享我的
体验。

I don't have any example code, but I have written several Python applications (and one C++ app) using plain SQL and have started work on a new app using SQLAlchemy, so I'll share my experience.

让我补充说,我花了几年维护一个适度大小的
SQL数据库,并写了大量的纯SQL,虽然不一定使用
Python。

Let me add that I spent several years maintaining a moderately sized SQL database and wrote lots of pure SQL, though not necessarily using Python.

当我的程序访问SQL时,我发现自己写了很多SQL代码
来访问数据。很多这样的代码虽然不完全相同,却是非常
类似的,似乎多余。例如,取一个简单的单表
查找。如果你想做简单的CRUD(创建,更新和删除),你
必须为每个表写至少三个单独的SQL语句。
虽然这些SQL语句的框架是类似的,但特定的
列名称和表名称是不同的。我最后写了一些
Python例程,如果我提供了一个
列的列和表名,它将构建SQL语句。但是这是SQLAlchemy的
(和更多)所以为什么重新发明轮?

As my programs accessed SQL, I found myself writing a lot of SQL code to access the data. A lot of this code while not identical, was very similar and seemed redundant. For example, take a simple single table lookup. If you want to do simple CRUD (create, update & delete), you have to write at least three separate SQL statements for each table. While the skeleton of these SQL statements are similar, the specific column names and the table name are different. I ended up writing some Python routines that would build the SQL statements if I supplied a list of columns and the table name. But this is what SQLAlchemy does (and much more) so why reinvent the wheel?

在我的C ++应用程序的情况下合适的ORM),我结束了
编写一个Python脚本生成SQL语句和C ++代码
访问表。

In the case of my C++ app (I hadn't found a suitable ORM), I ended up writing a Python script to generate SQL statements and C++ code to access the tables.

另一个优点是相对轻松,可以处理
对数据库结构的更改。当我在开发我的SA应用程序时,I
意识到我需要一个新列(至少)我的一个表。
我使用SA的声明式方法,我只需要添加列
到我的声明。我不需要更改任何SQL或Python代码。

Another advantage is the relative ease with which you can handle changes to the database structure. As I was developing my SA app, I realized that I needed a new column in (at least) one of my tables. I'm using SA's declarative approach and I only had to add the column to my declarative. I didn't have to change any SQL or Python code.

一个问题我已经使用SQLAlchemy是不学习的方式我做了
的东西与纯SQL。在最简单的层次上,SQLAlchemy可以生成
的SQL和Python代码来访问单个表,你可能会试图编写Python代码来合并这种单表访问
到更大的数据图,但SQLAlchemy的真实能力(IMHO)是
,它可以自动处理复杂的数据图形。

One "problem" I have had with SQLAlchemy is unlearning the way I did things with pure SQL. At it's simplest level, SQLAlchemy can generate the SQL and Python code to access single tables and you might be tempted to write Python code to merge this kind of single table access into larger data graphs, but the true power (IMHO) of SQLAlchemy is that it can handle complex data graphs automatically.

标记

这篇关于为什么要使用SQLAlchemy?它是非常有说服力的编码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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