简体   繁体   English

如何在sqlalchemy中返回执行原始查询

[英]How to execute a raw query with returning in sqlalchemy

I have a table Ticket that has the id (autoincremental), ticket_number (trigger that reads a sequence), value and date in Oracle. 我有一个表Ticket ,在Oracle中具有ID (自动递增), ticket_number (读取序列的触发器), 日期的 票证 And I want to do the following: 我要执行以下操作:

INSERT INTO TICKET (value, date) values (100, TO_DATE('07-29-2015', 'mm-dd-yyyy')) returning ticket_number into :ticket_number;

I need to do this in raw SQL, but I don't know how to get the value in sqlalchemy. 我需要在原始SQL中执行此操作,但是我不知道如何在sqlalchemy中获取值。 is it possible? 可能吗?

I've tried this with a toy table in Postgres and it works, I think should be equivalent in Oracle, please let me know. 我已经在Postgres的玩具表中尝试过此方法,并且它可以工作,我认为在Oracle中应该是等效的,请告诉我。

In [15]:

result = session.connection().execute("insert into usertable values('m6', 'kk2', 'Chile') returning username")
for r in result:
    print r
(u'm6',)

Hope it helps. 希望能帮助到你。

EDIT for Oracle : the one way to do it that I've found is not very elegant. 针对Oracle的编辑 :我发现的一种实现方法不是很优雅。 It would be using the raw connection underneath SQLAlchemy connection, something like: 它将使用SQLAlchemy连接下面的原始连接,如下所示:

In [15]:

from sqlalchemy.sql import text
import cx_Oracle
​
cur = session.connection().connection.cursor()
out = cur.var(cx_Oracle.STRING)
par = { "u" : out }   ​
cur.prepare("insert into usertable values('m34', 'kk2', 'Chile') returning username into :u")
cur.execute(None, par)
​
print(out)
print(type(out))
print(out.getvalue())
​
​
<cx_Oracle.STRING with value 'm34'>
<type 'cx_Oracle.STRING'>
m34

Unfortunately, I don't think there is a way to create a cx_oracle variable instance, it is just not available in the api, see docs . 不幸的是,我不认为有一种创建cx_oracle variable实例的方法,只是在api中不可用,请参阅docs

Then, there is no way to avoid creating the cursor, even if it works when you delegate more to SQLAlchemy : 然后,即使您将游标委托给SQLAlchemy ,也无法避免创建游标:

In [28]:

from sqlalchemy.sql import text
import cx_Oracle
​
cur = session.connection().connection.cursor()
out = cur.var(cx_Oracle.STRING)
par = { "u" : out }
​
q = text("insert into usertable values('m43', 'kk2', 'Chile') returning username into :u")
result = session.connection().execute(q, par)
print(par["u"])
print(out)
type(out)

​<cx_Oracle.STRING with value 'm43'>
<cx_Oracle.STRING with value 'm43'>
Out[28]:
cx_Oracle.STRING

Of course, you should close the cursor in this second case (in the first one, oracle closes it). 当然,在第二种情况下,您应该关闭游标(在第一种情况下,oracle将其关闭)。 The point that there is no way to create an instance like out = cx_Oracle.STRING() 没有办法像out = cx_Oracle.STRING()那样创建实例这一点

As I say, it is not very elegant, but I don't think there is a way to create an equivalent variable in SQLAlchemy . 正如我说的那样,它不是很优雅,但是我认为没有办法在SQLAlchemy创建等效变量。 It is something that the code handles internally. 这是代码内部处理的事情。 I would just go for the raw connection-cursor. 我只是去找原始的连接光标。

Hope it helps. 希望能帮助到你。

EDIT2 : In the code above, added out.getvalue() as suggested. EDIT2 :在上面的代码中,根据建议添加out.getvalue() Thanks! 谢谢!

You should be able to use the execute command. 您应该能够使用execute命令。 Something like this: 像这样:

raw_SQL = "INSERT INTO TICKET (value, date) values (100, TO_DATE('07-29-2015', 'mm-dd-yyyy')) returning ticket_number into :ticket_number;"
connection = engine.connect()
result = connection.execute(raw_SQL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM