简体   繁体   English

如何在SQLAlchemy中连接来自不同数据库的2个表?

[英]How to join 2 tables from different databases in SQLAlchemy?

I am using python/SQLAlchemy to extract data from MySQL databases. 我正在使用python / SQLAlchemy从MySQL数据库提取数据。 I have 2 different hosts, each one with a database, and I need to join 2 tables (one in each host/database). 我有2个不同的主机,每个主机都有一个数据库,我需要连接2个表(每个主机/数据库中有一个)。 How can I do it? 我该怎么做?

I'm reading this documentation but I couldn't get any straightforward help. 我正在阅读本文档,但无法获得任何直接帮助。 Connecting to one database is as simple as: 连接到一个数据库很简单:

engine = create_engine('mysql+pymysql://user:pass@host/database')

But I'm not sure how I can work with two engines. 但是我不确定如何使用两个引擎。

Anybody? 有人吗 Thanks in advance. 提前致谢。

You could use Pandas as the glue between the two databases. 您可以将Pandas用作两个数据库之间的粘合剂 For example, 例如,

import config
import pandas as pd
import sqlalchemy as SA

engine_postgresql = SA.create_engine('postgresql+psycopg2://{u}:{p}@{h}/{d}'.format(
    u=PGUSER, p=PGPASS, h=PGHOST, d='pgtest'))
engine_mysql = SA.create_engine('mysql+mysqldb://{u}:{p}@{h}/{d}'.format(
    u=MYUSER, p=MYPASS, h=MYHOST, d='mytest'))

sql = 'SELECT col1, col2, col3 FROM tableA'
df1 = pd.read_sql(sql, con=engine_postgresql)
sql = 'SELECT col1, col2, col4 FROM tableB'
df2 = pd.read_sql(sql2, con=engine_mysql)

result = pd.merge(df1, df2, how='left', on=['col1', 'col2'])

pd.read_sql passes an SQL query to the database and returns a DataFrame. pd.read_sql将SQL查询传递给数据库并返回一个DataFrame。 pd.merge joins the two DataFrames and returns a DataFrame. pd.merge将两个DataFrame联接在一起并返回一个DataFrame。

DataFrames can also be inserted into databases as tables using the to_sql method . 也可以使用to_sql方法to_sql作为表插入数据库中。 For example, 例如,

result.to_sql('tablename', engine_postgresql, if_exists='append')

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

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