When performing SQL statements such as UPDATE
, and INSERT
, the usual .fetch*()
methods on the Cursor
instance obviously don't apply to the number of rows modified.
In the event of executing one of the aforementioned statements, what is the correct way to obtain the corresponding row count in Python, and the corresponding API in the Sqlite3 C interface?
How to find the number of rows in an SQLite3 SELECT statement containing the COUNT() function in Python. Finding the number of rows from executing an SQLite3 SELECT statement containing COUNT() gives how many rows satisfied the conditions specified in the statement.
int sqlite3_total_changes(sqlite3*); sqlite3_int64 sqlite3_total_changes64(sqlite3*); These functions return the total number of rows inserted, modified or deleted by all INSERT, UPDATE or DELETE statements completed since the database connection was opened, including those executed as part of trigger programs.
SQLite Python: Querying Data First, establish a connection to the SQLite database by creating a Connection object. Next, create a Cursor object using the cursor method of the Connection object. Then, execute a SELECT statement. After that, call the fetchall() method of the cursor object to fetch the data.
After calling your Cursor.execute*()
methods with your UPDATE or INSERT statements you can use Cursor.rowcount
to see the # of rows affected by the execute call.
If I had to guess I would say the python lib is calling int sqlite3_changes(sqlite3*)
from the C API but I have not looked at the code so I can't say for sure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With