【问题标题】:python sqlalchemy combine query from two tablespython sqlalchemy 组合来自两个表的查询
【发布时间】:2016-03-25 20:22:53
【问题描述】:

我有两个像这样不相关的表:

hurst = Table('Hurst', metadata,
    Column('symbol_id' , Integer, primary_key=True), 
    Column('Date'      , String(20), nullable=False),
    Column('symbol'    , String(40), nullable=False),
    Column('HurstExp'  , Float,      nullable=False),
)

fundamental = Table('Fundamental', metadata,
    Column('symbol_id' , Integer,    primary_key=True), 
    Column('Date'      , String(20), nullable=False),
    Column('symbol'    , String(40), nullable=False),
    Column('MarketCap' , Float,      nullable=False),
)

以下每个查询都可以正常工作。我如何将它们结合起来,以便我可以获得价值超过 50,000,000,000 的公司的利益?

# -*- coding: utf-8 -*-
"""
Created on Sun Dec 13 19:22:35 2015

@author: idf
"""

from sqlalchemy import *

def run(stmt):
    rs = stmt.execute()
    return rs

# Let's re-use the same database as before
dbh = create_engine('sqlite:///hurst.db')
dbf = create_engine('sqlite:///fundamental.db')

dbh.echo = True  # We want to see the SQL we're creating
dbf.echo = True  # We want to see the SQL we're creating

metadatah = MetaData(dbh)
metadataf = MetaData(dbf)

# The users table already exists, so no need to redefine it. Just
# load it from the database using the "autoload" feature.
hurst = Table('Hurst',       metadatah, autoload=True)
funda = Table('Fundamental', metadataf, autoload=True)

hurstQ = hurst.select(hurst.c.HurstExp < .5)
run(hurstQ)

fundaQ = funda.select(funda.c.MarketCap > 50000000000)
run(fundaQ)

如果我尝试使用联接,则会收到错误消息:

j = join(hurst, funda, hurst.c.symbol == funda.c.symbol)
stmt = select([hurst]).select_from(j)
theJoin = run(stmt)


Traceback (most recent call last):
  File "/home/idf/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1139, in _execute_context
    context)
  File "/home/idf/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/default.py", line 450, in do_execute
    cursor.execute(statement, parameters)

   cursor.execute(statement, parameters)
sqlite3.OperationalError: no such table: Fundamental

我什至不会做简单的版本

# This will return more results than you are probably expecting.
s = select([hurst, funda])
run(s)

【问题讨论】:

  • 查看编辑后的帖子。桌子是分开的。也许我需要以某种方式将它们关联起来,但是这两个表是分开创建的。
  • 您需要可以使用sqlalchemybackref 功能并使用外键或关系创建另一个表的引用。

标签: python sqlite python-3.x sqlalchemy


【解决方案1】:

目前还没有环境可以测试这个,但类似的东西应该可以工作:

j = join(husrt, funda, (hurst.c.symbol_id == funda.c.symbol_id) & (funda.c.MarketCap > 50000000000))
stmt = select([hurst]).select_from(j)
run(stmt)

this SQL Alchemy docs page. 上查看sqlalchemy.sql.expression.join

【讨论】:

  • 查看编辑后的帖子。即使是这么简单的事情似乎也行不通。
【解决方案2】:

我不相信你可以加入两个数据库

您的一个表在一个数据库中,另一个在另一个数据库中:

dbh = create_engine('sqlite:///hurst.db')
dbf = create_engine('sqlite:///fundamental.db')

您应该将所有表放在同一个database.db 文件中,并拥有一个db = create_engine('sqlite:///database.db')

更正:您可以这样做:Cross database join in sqlalchemy

但是您真的希望将每个表都放在单独的数据库中吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 2020-12-22
    • 2020-10-12
    相关资源
    最近更新 更多