当前位置: 首页 > 面试题库 >

使用SQLAlchemy连接两个数据库中的表

隆飞驰
2023-03-14
问题内容

我正在使用两个MySQL数据库。我想将DB1中的表与SQLAlchemy中DB2中的表连接起来。

我在sqlalchemy中创建数据访问层时正在使用automap_base,如下所示…

class DBHandleBase(object):

    def __init__(self, connection_string='mysql+pymysql://root:xxxxxxx@localhost/services', pool_recycle=3600):
            self.Base_ = automap_base()
            self.engine_ = create_engine(connection_string,
                                         pool_recycle = pool_recycle)
            self.Base_.prepare(self.engine_, reflect=True)
            self.session_ = Session(self.engine_)

我的表类就像

class T1D1_Repo():


    def __init__(self, dbHandle):
        # create a cursor
        self.Table_ = dbHandle.Base_.classes.t1
        self.session_ = dbHandle.session_

我正在这样加入

db1_handle = DB1_Handle()
db2_handle = DB2_Handle()
t1d1_repo = T1D1_Repo(handle)
t1d2_repo = T1D2_Repo(person_handle)

result = t1d1_repo.session_.query(
            t1d1_repo.Table_,
            t1d2_repo.Table_).join(t1d2_repo.Table_, (
                t1d1_repo.Table_.person_id
                == t1d2_repo.Table_.uuid))

我收到这样的错误:

sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (1146, "Table 'db1.t1d2' doesn't exist") [SQL: 'SELECT

我们在数据库db1中创建了表t1,在数据库db2中创建了表t2。

是否可以在sqlalchemy ORM中的两个数据库表之间进行联接?如何实现呢?


问题答案:

在MySQL中,数据库与模式同义。例如在Postgresql中,您可以在数据库中的多个模式之间进行查询,而不能直接在数据库之间进行查询,而在MySQL中,您可以在多个数据库之间进行查询,因为两者之间没有区别。

因此,在MySQL中对多数据库查询的一种可能的解决方案可能是使用单个引擎,会话和Base来处理您的模式并将schema关键字参数传递给表,或反映这两种模式以使其完全合格。

由于没有您的数据,因此我在名为sopython和sopython2的测试服务器上制作了2个架构(MySQL数据库):

mysql> create database sopython;
Query OK, 1 row affected (0,00 sec)

mysql> create database sopython2;
Query OK, 1 row affected (0,00 sec)

并在每个表中添加一个表:

mysql> use sopython
Database changed
mysql> create table foo (foo_id integer not null auto_increment primary key, name text);
Query OK, 0 rows affected (0,05 sec)

mysql> insert into foo (name) values ('heh');
Query OK, 1 row affected (0,01 sec)

mysql> use sopython2
Database changed
mysql> create table bar (bar_id integer not null auto_increment primary key, foo_id integer, foreign key (foo_id) references `sopython`.`foo` (foo_id)) engine=InnoDB;
Query OK, 0 rows affected (0,07 sec)

mysql> insert into bar (foo_id) values (1);
Query OK, 1 row affected (0,01 sec)

在Python中:

In [1]: from sqlalchemy import create_engine

In [2]: from sqlalchemy.orm import sessionmaker

In [3]: from sqlalchemy.ext.automap import automap_base

In [4]: Session = sessionmaker()

In [5]: Base = automap_base()

创建引擎而不指定您默认使用的架构(数据库):

In [6]: engine = create_engine('mysql+pymysql://user:pass@:6603/')

In [7]: Base.prepare(engine, reflect=True, schema='sopython')

In [8]: Base.prepare(engine, reflect=True, schema='sopython2')
/home/user/SO/lib/python3.5/site-packages/sqlalchemy/ext/declarative/clsregistry.py:120: SAWarning: This declarative base already contains a class with the same class name and module name as sqlalchemy.ext.automap.foo, and will be replaced in the string-lookup table.
  item.__name__

警告是我不完全了解的,可能是2个表之间的外键引用导致重新反射foo的结果,但似乎没有造成麻烦。

该警告是第二次调用的结果,该第二次调用是prepare()为第一次调用中反映的表重新创建和替换类的。避免所有情况的方法是,首先使用元数据从两个模式中反映表,然后准备:

Base.metadata.reflect(engine, schema='sopython')
Base.metadata.reflect(engine, schema='sopython2')
Base.prepare()

完成所有这些之后,您可以查询foo和bar的连接:

In [9]: Base.metadata.bind = engine

In [10]: session = Session()

In [11]: query = session.query(Base.classes.bar).\
    ...:     join(Base.classes.foo).\
    ...:     filter(Base.classes.foo.name == 'heh')

In [12]: print(query)
SELECT sopython2.bar.bar_id AS sopython2_bar_bar_id, sopython2.bar.foo_id AS sopython2_bar_foo_id 
FROM sopython2.bar INNER JOIN sopython.foo ON sopython.foo.foo_id = sopython2.bar.foo_id 
WHERE sopython.foo.name = %(name_1)s

In [13]: query.all()
Out[13]: [<sqlalchemy.ext.automap.bar at 0x7ff1ed7eee10>]

In [14]: _[0]
Out[14]: <sqlalchemy.ext.automap.bar at 0x7ff1ed7eee10>

In [15]: _.foo
Out[15]: <sqlalchemy.ext.automap.foo at 0x7ff1ed7f09b0>


 类似资料:
  • 我正在开发一个基于SAAS的站点,我必须将两个DBs中的两个表连接起来,比如说DB1中的table1和DB2中的table2。我必须使用cakephp中的join从表1和表2获取匹配记录,但它会抛出如下错误: 错误:SQLSTATE[42000]:语法错误或访问冲突:1142 SELECT命令拒绝用户'dbname'@'localhost'访问表'table_name'。 有谁能解释一下如何使用c

  • 问题内容: 这是我简单的测试脚本。只是试图做一个基本的选择语句。在教程中找到了基本知识。 经过一个小时的搜索并尝试了一些解决方案,我比开始时就更接近解决问题了。希望我在某个地方犯了一个简单的错误,但我找不到它… 这是我遇到的错误 任何帮助将非常感激! 问题答案: 如果未在URL中指定,则该方言的默认驱动程序将为“ SQL Server” [1]。这意味着您需要在/etc/unixODBC/odbc

  • 本文向大家介绍sqlalchemy 使用连接,包括了sqlalchemy 使用连接的使用技巧和注意事项,需要的朋友参考一下 示例 您可以使用上下文管理器打开连接(即从池中请求一个连接): 有无,但必须手动将其关闭:            

  • 请知道,我对数据库很陌生。我能够正确安装mySQL和java连接器驱动程序。但每当我在eclipse中运行程序并尝试从我创建的数据库中检索信息时,我都会收到以下消息:“需要SSL连接,但服务器不支持”。下面是我要使用安全SSL连接运行的代码: `公共静态void main(字符串[]参数){

  • 从bugu-mongo 2.11版本开始,支持连接到多个数据库。 在前面的示例代码中,我们都只是连接到一个数据库: //默认的数据库连接 BuguConnection conn = BuguFramework.getInstance().createConnection(); conn.setHost("192.168.0.100"); conn.setPort(27017); conn.setU

  • 假设我有两个数据帧,具有不同级别的信息,如下所示: 我想加入df1和df2,并将“值”信息传递给df2:一天中的每一小时都将获得“日”值。 预期产出: