Some libraries and sites (like kotti) expose a database session that is loaded from a configuration file (it uses pyramid).
In general you can ignore the driver for sqlalchemy, but there are a few issues like getting a random row and using timezones with sqlite, which require you to have specific behavior for different engines.
Thing is, I can't see to find out how to determine what driver you're using at run time.
How do you do this?
Specifically, how, from a session (not an engine or a session factory) can you work backwards and figure this out?
connect() method returns a Connection object, and by using it in a Python context manager (e.g. the with: statement) the Connection. close() method is automatically invoked at the end of the block. The Connection , is a proxy object for an actual DBAPI connection.
The Session begins in a mostly stateless form. Once queries are issued or other objects are persisted with it, it requests a connection resource from an Engine that is associated with the Session , and then establishes a transaction on that connection.
The Engine is the starting point for any SQLAlchemy application. It's “home base” for the actual database and its DBAPI, delivered to the SQLAlchemy application through a connection pool and a Dialect , which describes how to talk to a specific kind of database/DBAPI combination.
Session. commit() is used to commit the current transaction. It always issues Session. flush() beforehand to flush any remaining state to the database; this is independent of the “autoflush” setting.
If you do this
session.bind.dialect.name
it will return something like sqlite
or mysql
, i.e. the part at the beginning of a URL (mysql://...
). Most other information can also be fetched from the dialect
object if you are interested in more. You can find this on any engine
or connection
(which bind
is).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With