diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index 787660c893e4e0..db88889082d840 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -1463,6 +1463,20 @@ Connection objects Return the total number of database rows that have been modified, inserted, or deleted since the database connection was opened. + .. attribute:: changes + + Return the number of database rows that were modified, inserted, or + deleted by the most recently completed ``INSERT``, ``UPDATE``, or + ``DELETE`` statement on this connection. + Statements other than ``INSERT``, ``UPDATE``, or ``DELETE``, + such as ``SELECT``, leave the value unchanged. + An ``UPDATE`` or ``DELETE`` that matches no rows sets the value to zero. + See the `SQLite changes() documentation`_ for details. + + .. versionadded:: next + + .. _SQLite changes() documentation: https://www.sqlite.org/c3ref/changes.html + .. _sqlite3-cursor-objects: diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index 7a15ae5bcd2dbf..87e2f99967a744 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -808,6 +808,11 @@ sqlite3 :const:`~sqlite3.sqlite_version_info` is now a named tuple. (Contributed by Serhiy Storchaka in :gh:`157470`.) +* Add the :attr:`sqlite3.Connection.changes` attribute, which returns the + number of rows modified, inserted, or deleted by the most recently completed + ``INSERT``, ``UPDATE``, or ``DELETE`` statement. + (Contributed by Charles Machalow in :gh:`163106`.) + ssl --- diff --git a/Lib/test/test_sqlite3/test_dbapi.py b/Lib/test/test_sqlite3/test_dbapi.py index bbb7c1f8b61593..9703984120b3c9 100644 --- a/Lib/test/test_sqlite3/test_dbapi.py +++ b/Lib/test/test_sqlite3/test_dbapi.py @@ -609,6 +609,7 @@ def test_uninit_operations(self): funcs = ( lambda: self.cx.isolation_level, lambda: self.cx.total_changes, + lambda: self.cx.changes, lambda: self.cx.in_transaction, lambda: self.cx.iterdump(), lambda: self.cx.cursor(), @@ -1022,6 +1023,19 @@ def test_total_changes(self): self.cu.execute("insert into test(name) values ('foo')") self.assertLess(2, self.cx.total_changes, msg='total changes reported wrong value') + def test_changes(self): + self.cu.execute("delete from test") + self.cu.executemany("insert into test(name) values (?)", + [("a",), ("b",), ("c",)]) + self.assertEqual(self.cx.changes, 1) + self.cu.execute("update test set name = 'x' where name in ('a', 'b')") + self.assertEqual(self.cx.changes, 2) + # SELECT statements do not reset the count. + self.cu.execute("select * from test").fetchall() + self.assertEqual(self.cx.changes, 2) + self.cu.execute("delete from test") + self.assertEqual(self.cx.changes, 3) + # Checks for executemany: # Sequences are required by the DB-API, iterators # enhancements in pysqlite. diff --git a/Misc/NEWS.d/next/Library/2026-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst b/Misc/NEWS.d/next/Library/2026-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst new file mode 100644 index 00000000000000..83418f24c5555d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst @@ -0,0 +1,4 @@ +Add :attr:`sqlite3.Connection.changes`, which returns the number of rows +modified, inserted, or deleted by the most recently completed ``INSERT``, +``UPDATE``, or ``DELETE`` statement. +Patch by Charles Machalow. diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index f8bd3a8df35b66..494c81b05f328e 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -1815,6 +1815,20 @@ pysqlite_connection_get_total_changes(PyObject *op, void *Py_UNUSED(closure)) return PyLong_FromLong(sqlite3_total_changes(self->db)); } +static PyObject * +pysqlite_connection_get_changes(PyObject *op, void *Py_UNUSED(closure)) +{ + pysqlite_Connection *self = _pysqlite_Connection_CAST(op); + if (!pysqlite_check_connection(self)) { + return NULL; + } +#ifdef _PY_SQLITE_HAVE_CHANGES64 + return PyLong_FromLongLong(sqlite3_changes64(self->db)); +#else + return PyLong_FromLong(sqlite3_changes(self->db)); +#endif +} + static PyObject * pysqlite_connection_get_in_transaction(PyObject *op, void *Py_UNUSED(closure)) { @@ -2675,6 +2689,9 @@ static PyGetSetDef connection_getset[] = { {"isolation_level", pysqlite_connection_get_isolation_level, pysqlite_connection_set_isolation_level}, {"total_changes", pysqlite_connection_get_total_changes, NULL}, + {"changes", pysqlite_connection_get_changes, NULL, + PyDoc_STR("Number of rows modified, inserted, or deleted by the most " + "recently completed INSERT, UPDATE, or DELETE statement.")}, {"in_transaction", pysqlite_connection_get_in_transaction, NULL}, {"autocommit", get_autocommit, set_autocommit}, {"__text_signature__", get_sig, NULL}, diff --git a/PC/pyconfig.h b/PC/pyconfig.h index 9de3786a0c769b..86dae4734e77c3 100644 --- a/PC/pyconfig.h +++ b/PC/pyconfig.h @@ -730,6 +730,9 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */ #define HAVE_SETSOCKOPT 1 #define HAVE_SOCKET 1 +/* Define to 1 if you have a usable 'sqlite3_changes64' function. */ +#define _PY_SQLITE_HAVE_CHANGES64 1 + /* Define to 1 if you have the `dup' function. */ #define HAVE_DUP 1 diff --git a/configure b/configure index d8d860b84a370a..dd44f3ecaa2b43 100755 --- a/configure +++ b/configure @@ -18354,6 +18354,52 @@ printf "%s\n" "#define PY_SQLITE_HAVE_SERIALIZE 1" >>confdefs.h fi + CFLAGS="$CFLAGS $CFLAGS_NODIST" + LDFLAGS="$LDFLAGS $LDFLAGS_NODIST" + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for usable sqlite3_changes64" >&5 +printf %s "checking for usable sqlite3_changes64... " >&6; } +if test ${ac_cv_lib_sqlite3_sqlite3_changes64+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + + #include +int +main (void) +{ + + (void)sqlite3_changes64(0); + + ; + return 0; +} + +_ACEOF +if ac_fn_c_try_link "$LINENO" +then : + ac_cv_lib_sqlite3_sqlite3_changes64=yes +else case e in #( + e) ac_cv_lib_sqlite3_sqlite3_changes64=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam \ + conftest$ac_exeext conftest.$ac_ext + ;; +esac +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_sqlite3_sqlite3_changes64" >&5 +printf "%s\n" "$ac_cv_lib_sqlite3_sqlite3_changes64" >&6; } + if test "x$ac_cv_lib_sqlite3_sqlite3_changes64" = xyes +then : + + +printf "%s\n" "#define _PY_SQLITE_HAVE_CHANGES64 1" >>confdefs.h + + +fi else case e in #( e) diff --git a/configure.ac b/configure.ac index 11fd97ac5b765f..de436d63fdf6c7 100644 --- a/configure.ac +++ b/configure.ac @@ -4836,6 +4836,22 @@ dnl hence CPPFLAGS instead of CFLAGS. [Define if SQLite was compiled with the serialize API] ) ]) + dnl Reject APIs unavailable on the deployment target, even if they link. + CFLAGS="$CFLAGS $CFLAGS_NODIST" + LDFLAGS="$LDFLAGS $LDFLAGS_NODIST" + AC_CACHE_CHECK([for usable sqlite3_changes64], + [ac_cv_lib_sqlite3_sqlite3_changes64], [ + AC_LINK_IFELSE([ + AC_LANG_PROGRAM([#include ], [ + (void)sqlite3_changes64(0); + ]) + ], [ac_cv_lib_sqlite3_sqlite3_changes64=yes], + [ac_cv_lib_sqlite3_sqlite3_changes64=no]) + ]) + AS_VAR_IF([ac_cv_lib_sqlite3_sqlite3_changes64], [yes], [ + AC_DEFINE([_PY_SQLITE_HAVE_CHANGES64], [1], + [Define to 1 if you have a usable 'sqlite3_changes64' function.]) + ]) ], [ have_supported_sqlite3=no ]) diff --git a/pyconfig.h.in b/pyconfig.h.in index b0ac8a07019093..6fda555942d8df 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -2027,6 +2027,9 @@ /* Maximum length in bytes of a thread name */ #undef _PYTHREAD_NAME_MAXLEN +/* Define to 1 if you have a usable 'sqlite3_changes64' function. */ +#undef _PY_SQLITE_HAVE_CHANGES64 + /* Defined if _Complex C type can be used with libffi. */ #undef _Py_FFI_SUPPORT_C_COMPLEX