How can I access the number of rows affected by:
cursor.execute("SELECT COUNT(*) from result where server_state='2' AND name LIKE '"+digest+"_"+charset+"_%'")
How can I access the number of rows affected by:
cursor.execute("SELECT COUNT(*) from result where server_state='2' AND name LIKE '"+digest+"_"+charset+"_%'")
Try using fetchone
:
cursor.execute("SELECT COUNT(*) from result where server_state='2' AND name LIKE '"+digest+"_"+charset+"_%'")
result=cursor.fetchone()
result
will hold a tuple with one element, the value of COUNT(*)
.
So to find the number of rows:
number_of_rows=result[0]
Or, if you'd rather do it in one fell swoop:
cursor.execute("SELECT COUNT(*) from result where server_state='2' AND name LIKE '"+digest+"_"+charset+"_%'")
(number_of_rows,)=cursor.fetchone()
PS. It's also good practice to use parametrized arguments whenever possible, because it can automatically quote arguments for you when needed, and protect against sql injection.
The correct syntax for parametrized arguments depends on your python/database adapter (e.g. mysqldb, psycopg2 or sqlite3). It would look something like
cursor.execute("SELECT COUNT(*) from result where server_state= %s AND name LIKE %s",[2,digest+"_"+charset+"_%"])
(number_of_rows,)=cursor.fetchone()
?
syntax instead. –
Pronounce From PEP 249, which is usually implemented by Python database APIs:
Cursor Objects should respond to the following methods and attributes:
[…]
.rowcount
This read-only attribute specifies the number of rows that the last .execute*() produced (for DQL statements like 'select') or affected (for DML statements like 'update' or 'insert').
But be careful—it goes on to say:
The attribute is -1 in case no
.execute*()
has been performed on the cursor or the rowcount of the last operation is cannot be determined by the interface. [7]Note:
Future versions of the DB API specification could redefine the latter case to have the object returnNone
instead of -1.
So if you've executed your statement, and it works, and you're certain your code will always be run against the same version of the same DBMS, this is a reasonable solution.
COUNT(*)
. If you query SELECT name FROM result WHERE server_state='2'
, for example, you will get zero, one or multiple rows. –
Birr cursor.rowcount == -1
for SELECT
statements as it doesn't know how many rows will be returned until you've returned all rows; it's iterators all the way down for SQLite. –
Prognostication rowcount
can be -1
in case the the rowcount could not be determined, but this answer papers over that caveat. I had to check if that was a later addition, but rowcount not always being set has been part since the initial PEP commit from 2001. –
Prognostication None
in future, I now think this isn't actually a good solution for my case. So your suggestion has saved me from possible future bugs. –
Fowliang The number of rows effected is returned from execute:
rows_affected=cursor.execute("SELECT ... ")
of course, as AndiDog already mentioned, you can get the row count by accessing the rowcount property of the cursor at any time to get the count for the last execute:
cursor.execute("SELECT ... ")
rows_affected=cursor.rowcount
From the inline documentation of python MySQLdb:
def execute(self, query, args=None):
"""Execute a query.
query -- string, query to execute on server
args -- optional sequence or mapping, parameters to use with query.
Note: If args is a sequence, then %s must be used as the
parameter placeholder in the query. If a mapping is used,
%(key)s must be used as the placeholder.
Returns long integer rows affected, if any
"""
cursor.execute
method is no longer defined (in previous version of the specification it was expected to work as in Boaz' example). The specification explicitly suggests using of the the more flexible .rowcount attribute instead as the value returned by the execute
method is database interface implementation dependent. –
Chaiken cursor.rowcount
may be left at -1
. The PEP specification leaves this up to the database adapter, so it is not guaranteed to work for all databases. The return value of cursor.execute()
is not prescribed by the PEP, and some database drivers (such as sqlite3
) return the cursor, not a row count. –
Prognostication In my opinion, the simplest way to get the amount of selected rows is the following:
The cursor object returns a list with the results when using the fetch commands (fetchall(), fetchone(), fetchmany()). To get the selected rows just print the length of this list. But it just makes sense for fetchall(). ;-)
print len(cursor.fetchall)
# python3
print(len(cur.fetchall()))
cursor.fetchall
here, so the literal code, as written, produces a type error. And why fetch all rows just to use len()
on the list? That's just a waste of resources. Use SELECT COUNT(*)
to have the server do the counting. –
Prognostication To get the number of selected rows I usually use the following:
cursor.execute(sql)
count = len(cursor.fetchall())
when using count(*)
the result is {'count(*)': 9}
-- where 9 represents the number of rows in the table, for the instance.
So, in order to fetch the just the number, this worked in my case, using mysql 8.
cursor.fetchone()['count(*)']
The accepted answer is fine, but here's how you can get it in one line after executing the cursor:
result = cur.fetchone()[0]
© 2022 - 2024 — McMap. All rights reserved.
COUNT(*)
which means the actual question is "How to access the result ofcursor.execute
. – Pronounce