连接到 Oracle 数据库,我需要运行以下查询:
SELECT
ad_campaign_id as campaign_id,
TO_DATE("DAY", 'YYYY-MM-DD') as DAY,
NVL(spent, 0) as spent,
impressions,
clicks
FROM sometable
我怎样才能用 SQLAlchemy 做到这一点?如何将参数发送到func.to_date
函数?
连接到 Oracle 数据库,我需要运行以下查询:
SELECT
ad_campaign_id as campaign_id,
TO_DATE("DAY", 'YYYY-MM-DD') as DAY,
NVL(spent, 0) as spent,
impressions,
clicks
FROM sometable
我怎样才能用 SQLAlchemy 做到这一点?如何将参数发送到func.to_date
函数?
SQLAlchemy 的func将接受任何 Oracle 函数作为属性。
from sqlalchemy import func
campaign_id = sometable.c.ad_campaign_id
day = func.to_date(sometable.c.day, 'YYYY-MM-DD') # Is this what you need?
spent = func.nvl(sometable.c.spent, 0)
clicks = sometable.c.clicks
cols = [campaign_id, day, spent, clicks]
# Feel free to skip, this is just so you get the labels you want
labels = "campaign_id day spent clicks".split()
col_label = zip(cols, labels)
labelled = [c.label(l) for (c,l) in col_label]
q = session.query(*labelled) # or use *cols if you don't care about labels
q.all()
以下是执行任意查询的方式:
engine.execute(query_string)
设置引擎是另一回事。虽然它有很好的记录。
如果您再次询问如何使用一些不同的模型来做到这一点......
所以这是你问题的第一部分回答(可能)。但是 func.to_date 函数是什么意思?请澄清