Skip to content

Commit

Permalink
pythongh-79579: Update sqlite3.Cursor.rowcount for all datamodifying …
Browse files Browse the repository at this point in the history
…queries
  • Loading branch information
erlend-aasland committed Jun 6, 2022
1 parent 86a5e22 commit e82adb8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,13 @@ def test_rowcount_executemany(self):
self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)])
self.assertEqual(self.cu.rowcount, 3)

def test_rowcount_prefixed_with_comment(self):
# gh-79579: rowcount is updated even if query is prefixed with comments
self.cu.execute("/* foo */ insert into test(name) values (?)", ('foo',))
self.assertEqual(self.cu.rowcount, 1)
self.cu.execute("/* bar */ update test set name='bar' where name='foo'")
self.assertEqual(self.cu.rowcount, 2)

def test_total_changes(self):
self.cu.execute("insert into test(name) values ('foo')")
self.cu.execute("insert into test(name) values ('foo')")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:data:`sqlite3.Cursor.rowcount` is now correctly updated for all datamodifying
SQL queries. Patch by Erlend E. Aasland.
2 changes: 1 addition & 1 deletion Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
}
}

if (self->statement->is_dml) {
if (!sqlite3_stmt_readonly(self->statement->st)) {
self->rowcount += (long)sqlite3_changes(self->connection->db);
} else {
self->rowcount= -1L;
Expand Down

0 comments on commit e82adb8

Please sign in to comment.