使用 SQLAlchemy 创建存储过程 [英] Creating Stored Procedures with SQLAlchemy

查看:65
本文介绍了使用 SQLAlchemy 创建存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 python 脚本来使用 SQLAlchemy 创建 postgres 数据库.我也想用同样的方式创建存储过程.我检查了 SQL Alchemy 文档,但无法找到是否可以使用它创建存储过程.有可能这样做吗?任何教程/示例都会有所帮助.我找到了一些关于如何使用 SQLAlchemy 调用 SP 的示例,但没有找到有关如何创建的示例.

I am writing a python script to create the postgres database using SQLAlchemy. I also want to create Stored Procedures by same way. I checked the SQL Alchemy Documentations but was not able to find whether I can create stored procedure using it or not. Is it Possible to do so? any Tutorials/Examples would help. i found some examples of how to call SP using SQLAlchemy but not about how to create.

提前致谢.塔拉·辛格

推荐答案

您可以使用 存储过程(实际上,执行任何有效的SQL语句)="http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.text" rel="noreferrer">sqlalchemy.sql.expression.text 构造:

You can create stored procedures (actually, execute any valid SQL statement) by using sqlalchemy.sql.expression.text construct:

t = text("SELECT * FROM users WHERE id=:user_id")
result = connection.execute(t, user_id=12)

但这更像是 SQLAlchemy 的附录,而不是设计好的用法.
此外,这不能以独立于 DMBS 的方式完成,这是使用 ORM 工具(如 SQLAlchemy)的好处之一.
如果你的目标是对你的数据库模式进行版本控制,你仍然可以使用它,但你需要完全控制这个过程并处理诸如存储过程UDFs、views 并以正确的顺序创建/删除它们.

But this will be more of an appendix to the SQLAlchemy rather than designed usage.
Also this cannot be done in a DMBS-independent way, which is one of the benefits using ORM tools like SQLAlchemy.
If your aim is to version-control your database schema, you still can use it, but you need to take complete control this process and handle things like dependencies between stored procedures, UDFs, views and create/drop them in the proper order.

这篇关于使用 SQLAlchemy 创建存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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