SQLAlchemy and Multiple Databases

后端 未结 2 726
醉酒成梦
醉酒成梦 2021-01-30 17:46

I have an assortment of similar (but not identical) databases, and would like to use SQLAlchemy as a way to \"standardize\" access. The databases can differ very slightly, such

2条回答
  •  广开言路
    2021-01-30 18:27

    Your solution looks pretty good. Here's what I did.

    I have a package named connectors, and in it a module for each db as well as a settings file.

    Each of these connector modules creates its connection string and its engine, along with the declarative base and classes for the tables.

    Then there is a method loadSession that returns the session (this one I got from a tutorial or another post here somewhere, cant recall exactly) and another one I added that returns the engine in case I want to do something with that.

    So then in some other module of the program, I would do something like this

    from connectors import x, y, z
    
    x_ses = x.loadSession()
    y_ses = y.loadSession()
    z_ses = z.loadSession()
    
    xq = x_ses.query(...)
    yq = y_ses.query(...)
    

提交回复
热议问题