Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify SqlAlchemy engine object

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?

like image 545
alphanumeric Avatar asked Jan 27 '17 05:01

alphanumeric


People also ask

What is SQLAlchemy Engine?

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.

How do you close an Engine in SQLAlchemy?

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() .

Is SQLAlchemy Engine thread safe?

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.

What is the SQLAlchemy engine?

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.

How does SQLAlchemy connect to the database?

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:

How to check if SQLAlchemy is installed in Python?

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.

What is SQL alchemy create_engine in SQL Server?

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.


1 Answers

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)")
like image 110
Stephen Rauch Avatar answered Oct 14 '22 08:10

Stephen Rauch