Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of affected rows in sqlalchemy?

I have one question concerning Python and the sqlalchemy module. What is the equivalent for cursor.rowcount in the sqlalchemy Python?

like image 400
honzas Avatar asked Mar 31 '09 18:03

honzas


People also ask

What does SQLAlchemy all () return?

As the documentation says, all() returns the result of the query as a list.

What is _sa_instance_state in SQLAlchemy?

_sa_instance_state is a non-database-persisted value used by SQLAlchemy internally (it refers to the InstanceState for the instance. While not directly relevant to this section, if we want to get at it, we should use the inspect() function to access it).

What is aliased in SQLAlchemy?

Implementing alias in SQLAlchemy SQL alias is a method of giving a temporary name for a table that is more convenient and readable. SQL alias facilitates a simple name to be used in place of a complex table name when it has to be used multiple times in a query.

How many components are present in SQLAlchemy?

SQLAlchemy consists of two distinct components, known as the Core and the ORM.


2 Answers

ResultProxy objects have a rowcount property as well.

like image 53
DNS Avatar answered Sep 29 '22 04:09

DNS


rowcount is not the number of affected rows. Its the number of matched rows. See what doc says

This attribute returns the number of rows matched, which is not necessarily the same as the number of rows that were actually modified - an UPDATE statement, for example, may have no net change on a given row if the SET values given are the same as those present in the row already. Such a row would be matched but not modified. On backends that feature both styles, such as MySQL, rowcount is configured by default to return the match count in all cases

So for both of the following scenarios rowcount will report 1. Because of Rows matched: 1

  1. one row changed with update statement.

     Query OK, 1 row affected (0.00 sec)  Rows matched: 1  Changed: 1  Warnings: 0 
  2. same update statement is executed.

     Query OK, 0 row affected (0.00 sec)  Rows matched: 1  Changed: 0  Warnings: 0 
like image 20
Shiplu Mokaddim Avatar answered Sep 29 '22 03:09

Shiplu Mokaddim