From e82adb855f04788f1bc549b9eb76718b785e8d27 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" <erlend.aasland@protonmail.com> Date: Mon, 6 Jun 2022 12:59:34 +0200 Subject: [PATCH] gh-79579: Update sqlite3.Cursor.rowcount for all datamodifying queries --- Lib/test/test_sqlite3/test_dbapi.py | 7 +++++++ .../Library/2022-06-06-12-58-27.gh-issue-79579.e8rB-M.rst | 2 ++ Modules/_sqlite/cursor.c | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-06-06-12-58-27.gh-issue-79579.e8rB-M.rst diff --git a/Lib/test/test_sqlite3/test_dbapi.py b/Lib/test/test_sqlite3/test_dbapi.py index 1fa02db3b3af41..ea0c9f1aaa3ee9 100644 --- a/Lib/test/test_sqlite3/test_dbapi.py +++ b/Lib/test/test_sqlite3/test_dbapi.py @@ -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')") diff --git a/Misc/NEWS.d/next/Library/2022-06-06-12-58-27.gh-issue-79579.e8rB-M.rst b/Misc/NEWS.d/next/Library/2022-06-06-12-58-27.gh-issue-79579.e8rB-M.rst new file mode 100644 index 00000000000000..3c61b7e50365cb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-06-06-12-58-27.gh-issue-79579.e8rB-M.rst @@ -0,0 +1,2 @@ +:data:`sqlite3.Cursor.rowcount` is now correctly updated for all datamodifying +SQL queries. Patch by Erlend E. Aasland. diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index c58def5f0362f1..c238e6ec5ecb5f 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -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;