On Feb 16, 2014, at 6:09 PM, Alexander Perepelica <perepelica.a...@gmail.com> wrote:
> Hello! > > I use sqlalchemy in flask app and try create some database editor. > And my problem is using sqlalchemy models and give to user possibility change > IP address of database server (or connect to server at runtime). > Can I do such things with sqlalchemy? SQLAlchemy uses the Python DBAPI to connect to a database, see http://www.python.org/dev/peps/pep-0249/. So as far as the details of TCP/IP connections, SQLAlchemy just passes that information on to the DBAPI. For example, connecting to Postgresql with psycopg2: engine = create_engine(“postgresql+psycopg2://scott:tiger@somehostname/test”) sqla_conn = engine.connect() the above does the same as: import psycopg2 conn = psycopg2.connect(user=‘scott’, passwd=‘tiger’, host=‘somehostname’, database=‘test’) So, whatever hostname you put into “create_engine()” there, that’s the one that engine will connect to. If then you had an application where you want to at some point change to a different hostname, you’d make a new engine. you can also dispose() the old one engine.dispose() engine = create_engine(“postgresql+psycopg2://scott:tiger@newhostname/test”) The reason we call dispose() is because connections are by default pooled, meaning when you say: sqla_conn.close() that just returns the “connection” to a pool. You can *turn that off* by using NullPool: from sqlalchemy.pool import NullPool engine = create_engine(“postgresql+psycopg2://scott:tiger@somehostname/test”, poolclass=NullPool) with the above, you can make a new engine later and you don’t need to dispose() the old one, just make sure whatever connections were opened were closed.
signature.asc
Description: Message signed with OpenPGP using GPGMail