sqlalchemy 同时连接多个数据库。完成flask_sqlalchemy的__bind_key__功能

本文介绍了如何使用SQLAlchemy在Flask应用中同时连接并操作多个数据库,包括通过`__bind_key__`实现数据库绑定,以及通过`engine.binds`配置不同数据库连接。示例代码展示了具体的配置和使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请标明地址https://blog.youkuaiyun.com/weixin_42068117/article/details/83857355

 

https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/sql/schema.py

 

 """Symbol indicating that a :class:`.Table` or :class:`.Sequence`
    should have 'None' for its schema, even if the parent
    :class:`.MetaData` has specified a schema.
    .. versionadded:: 1.0.14
 """

def _get_table_key(name, schema):
    if schema is None:
        return name
    else:
        return schema + "." + name

       这一段代码显示的是我们给定的schema会拼接数据库的名字。大家应该可以理解是   数据库.表名  这样就不会有数据库的限制。这一段属于示例给大家理解的,如果只想知道怎么用,直接看本文最后的代码示例就可以了。

class Table(DialectKWArgs, SchemaItem, TableClause):

"""
 The :class:`.Table` object constructs a unique instance of itself based
    on its name and optional schema name within the given
    :class:`.MetaData` object. Calling the :class:`.Table`
    constructor with the same name and same :class:`.MetaData` argument
    a second time will return the *same* :class:`.Table` object - in this way
    the :class:`.Table` constructor acts as a registry function.

  :param schema: The schema name for this table, which is required if
        the table resides in a schema other than the default selected schema
        for the engine's database connection.  Defaults to ``None``.
        If the owning :class:`.MetaData` of this :class:`.Table` specifies
        its own :paramref:`.MetaData.schema` parameter, then that schema
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值