Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get number of modified rows after sqlite3 execute in Python

Tags:

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?

like image 514
Matt Joiner Avatar asked Feb 23 '10 03:02

Matt Joiner


People also ask

How do I count the number of rows in SQLite python?

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.

Which returns the total number of DB rows that been modified updated or deleted?

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.

How fetch data from SQLite3 in Python?

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.


1 Answers

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.

like image 166
mockobject Avatar answered Oct 24 '22 21:10

mockobject