连接到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
函数?
答案 0 :(得分:1)
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()
答案 1 :(得分:0)
以下是执行任意查询的方法:
engine.execute(query_string)
设置引擎是另一回事。尽管如此,它还是有很好的记录。
如果你问的是如何使用某个不同故事的模型来做到这一点......
这样你问题的第一部分(可能)得到了解答。但你的func.to_date函数是什么意思?请澄清