gpt4 book ai didi

python - 为什么要使用 SQLAlchemy?编码很方便吗?

转载 作者:IT老高 更新时间:2023-10-28 21:43:03 25 4
gpt4 key购买 nike

就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the help center为指导。




8年前关闭。




由于非常熟悉用 Python 编写简单的 SQL,我还没有意识到使用 SQLAlchemy 或其他 ORM 会带来什么好处。

它是为那些不喜欢程序中的 SQL 的人设计的吗?或者它让你的脚本跨越不同类型的数据库?

或者它让你的脚本更加 Pythonic?

或者它对于处理底层数据库的某些任务是否非常方便,并且它会自动为您完成很多琐碎的事情,因此您只需编写2-3行调用SQLAlchemy的代码,它就会为您完成所有琐碎的数据库操作?

如果有人可以提供使用纯 SQL 与使用 SQLALchemy 进行演示的代码并展示优于纯 SQL 的优势,那将是非常感激的。

谢谢!

最佳答案

你检查了吗?
https://groups.google.com/forum/?fromgroups=#!topic/sqlalchemy/VXXB12-3JCY

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

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

粘贴上面谷歌群组链接的回复:

Mark Erbaugh 22/11/2010

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.

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.

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?

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.

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.

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.

Mark

关于python - 为什么要使用 SQLAlchemy?编码很方便吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15426104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com