Python: Number of rows affected by cursor.execute("SELECT ...")
Retrieve the row count after a SELECT
query by utilizing cursor.rowcount
just after cursor.execute()
. Here it is in action:
Note: If cursor.rowcount
gives -1
or None
, it signifies that the count is unavailable. This behavior varies with the database driver in use.
Observing query safety
When issuing SELECT
statements, consider personal safety. Just like you wouldn't go out without a helmet, don't open your program to SQL injection. Use parametrized queries like below:
Place your trust in placeholders such as %s
or %(key)s
, and let the DB adapter deal with the quoting and escaping.
Getting row count efficiently
There could be times when you want the count but are in no mood to fetch all rows. Say you're tired, it's been a long day. You can do this:
- Just do
fetchone()
on aCOUNT
query:
For more than one result, fetchall()
or fetchmany(size)
are your friends, with len()
ready to reveal the count.
Adapting to your environment
Different strokes for different folks. Understand the parametrized arguments syntax that clicks with your Python/DB adapter. Below is an example for sqlite3:
Steering through common issues
Trusting rowcount
cursor.rowcount
may be not everyone's cup of tea. Its behavior has no guarantee by the DB API. Always take a quick look at your database driver's documentation.
Handling the big fish
When you face large datasets, using fetchall()
may overwhelm you. Think memory and processing. Use fetchone()
or fetchmany()
in loops to process results in portions:
Knowing your tools
Different databases offer various cursor types, such as server-side or client-side. This choice can affect the rowcount
.
Staying informed
Stay alert to future DB API changes. Like new tax rules, these might affect how rowcount operates or what it returns when the count is undefined.
Embracing the unknown
Understanding rowcount variation
Different database adapters may define rowcount
differently, especially after a SELECT
statement. Be agile and always check your adapter's documentation.
Crafting secure SQL strings
Be vigilant when string concatenation is involved, as it can pave way for SQL injection attacks. Stay safe and use parametrized queries.
Choosing the right fetch strategy
Depending on your use case and resources, choose between fetchone()
, fetchmany()
, or fetchall()
- like picking the right horse for the race.
Was this article helpful?