Home > Net >  How to get some kind 'execute' info/ status from SQLAlchemy?
How to get some kind 'execute' info/ status from SQLAlchemy?

Time:08-02

The topic related Software is use.

  • Python 3.9.13
  • psycopg2-binary 2.9.3
  • SQLAlchemy 1.4.37
  • PostgreSQL 13

I wrote a python script to automate some daily repetition work on my database, so far everything works. Nevertheless i want that SQLAlchemy is more talkative to me.

For instance if i do someUPDATE work on my table in the column client_label via the PGadmin 4 Query Tool:

pgAdmin


UPDATE webserver_clients
SET client_label = 'unknown';

The response of PGadmin is some kind of status/ info

UPDATE 93

Query returned successfully in 104 msec.

93 Datapoints updated and i took 104 msec. Nice! Such kind of status/ info would be nice to fetch via SQLAlchemy to integrate into some kind of further evaluation and logging in my script. The msecinfo is may not the most important for me, but things like the updated 93 Datapoints are valuable for me.

So i tried to look into the connection.execute() object like satus_info.__dict__ or satus_info.keys() but there is so many nested stuff going on. The Examples in the documentation that i saw were only related to queries with responding rows which is of course 0.

script.py


<...>
query = sa.text(f'''
    UPDATE webserver_clients
    SET client_label = 'unknown';''')
satus_info = connection.execute(query)
<...>

Question


So my Question is how can i retrieve some more Information or Status about the UPDATE process after it was executed from SQLAlchemy?

CodePudding user response:

attribute sqlalchemy.engine.BaseCursorResult.rowcount

This returns the rowcount for this result. This is what is matched by the query, not necessarily how many rows were modified.

The rowcount reports the number of rows matched by the WHERE criterion of an UPDATE or DELETE statement.

https://docs.sqlalchemy.org/en/14/core/connections.html#sqlalchemy.engine.ResultProxy.rowcount

  • Related