Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 17 additions & 0 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -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},
Expand Down
3 changes: 3 additions & 0 deletions PC/pyconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 46 additions & 0 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sqlite3.h>], [
(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
])
Expand Down
3 changes: 3 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading