zoukankan      html  css  js  c++  java
  • pylons使用多个数据库(multiple DB)

    最近做的工程要修改成两个数据库的,一个测试数据库, 一个线上数据库。

    所以就要把原来的只有一个数据库的改成两个数据库。

    第一步:修改development.ini

    # SQLAlchemy database URL
    sqlalchemy.test.url = mysql://username:password@host:port/database
    sqlalchemy.test.pool_recycle = 3600
    sqlalchemy.online.url = mysql://username:password@host:port/database
    sqlalchemy.online.pool_recycle = 3600

    一个测试数据库,一个线上数据库


    第二步:修改config/envirment.py

    # Setup the SQLAlchemy database engine
    test_engine = engine_from_config(config, 'sqlalchemy.test.')
    online_engine = engine_from_config(config, 'sqlalchemy.online.')
    init_model(test_engine, online_engine)

    第三步:修改model/meta.py

    """SQLAlchemy Metadata and Session object"""
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import scoped_session, sessionmaker
    __all__ = ['BaseTest', 'BaseOnline', 'Session']
    # SQLAlchemy session manager. Updated by model.init_model()
    Session = scoped_session(sessionmaker())

    # The declarative Base
    BaseTest = declarative_base()
    BaseOnline = declarative_base()

    第四步:修改model/__init__.py

    """The application's model objects"""
    from hello.model.meta import Session, BaseTest, BaseOnline

    from hello.model.template_test import Template_test
    from hello.model.template_online import Template_online

    def init_model(test_engine, online_engine):
        """Call me before using any of the tables or classes in the model"""
        meta.BaseTest.metadata.bind = test_engine
        meta.BaseOnline.metadata.bind = online_engine

    第五步:修改model/的多个表文件

    from sqlalchemy import Column
    from sqlalchemy import types
    from hello.model.meta import BaseTest
    class Template_test(BaseTest):
        __tablename__ = "template"

    第六步:修改controller

    根据model中表的情况修改controller的文件头

    from hello.model.template_test import Template_test

    大功告成!


  • 相关阅读:
    SQL Server 2005 上安装SQL Server Management Studio
    小心博客被Google点名为有恶意软件
    新文章尚邮使用评论 ,包含Gmail的设置以及存在的一些问题
    发布一小软件
    在 ASP.NET 上实现锁定表头、支持滚动的表格的做法
    怎样检测网络中的电脑是否有安装SQL 2000
    危险字符过滤的类
    通过避免下列 10 个常见 ASP.NET 缺陷使网站平稳运行(转载)
    javascript控制页面控件隐藏显示的两种方法
    整理的一些Tsql(二)
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3289821.html
Copyright © 2011-2022 走看看