I can declare engine
object with the invalid username, password or address and get no exception or error:
from sqlalchemy import create_engine
engine = create_engine('mysql://nouser:[email protected]')
print engine
it prints likes it is a valid engine object:
Engine(mysql://nouser:***@123.456.789)
What would be a common way to verify (to check) if the engine
object is valid or if it is "connectable" to db?
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.
If you're using any kind of "connectionless" execution, that is engine. execute() or statement. execute() , the ResultProxy object returned from that execute call should be fully read, or otherwise explicitly closed via close() .
Every pool implementation in SQLAlchemy is thread safe, including the default QueuePool . This means that 2 threads requesting a connection simultaneously will checkout 2 different connections. By extension, an engine will also be thread-safe.
The Engine is the starting point for any SQLAlchemy application. The engine combines a Pool and a Dialect and provides a way to connect to and interact with a database. A connection pool is a set of active long-running database connections that can be efficiently reused.
Your engineinstance only connects to the database when needed, and sqlalchemy just passes the connection info along to the driver specified in the url which returns a connection that sqlalchemy uses. Forgive that this is mysql, but should be fundamentally the same for you:
To check if SQLAlchemy is properly installed and to know its version, enter the following command in the Python prompt − >>> import sqlalchemy >>>sqlalchemy.__version__ '1.2.7' SQLAlchemy Core – Expression Language SQLAlchemy core includes SQL rendering engine, DBAPI integration, transaction integration, and schema description services.
It may be varied upon the requirement that SQL Alchemy is supported all the required databases and perform user-friendly operations in the environment. The sqlalchemy create_engine is one of the initial and basic steps to perform the database transactions.
Question: How to verify if the engine object is "connectable"?
From the (DOCs):
Note that the Engine and its underlying Pool do not establish the first actual DBAPI connection until the Engine.connect() method is called, or an operation which is dependent on this method such as Engine.execute() is invoked. In this way, Engine and Pool can be said to have a lazy initialization behavior.
So, to test if the engine object is "connectable" one needs to either explicitly call Engine.connect()
, or attempt to use the engine in some other way.
from sqlalchemy import create_engine
engine = create_engine('mysql://nouser:[email protected]')
engine.connect()
Will raise the error:
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2005, "Unknown MySQL server host '123.456.789' (0)")
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