It works!!! Thanks a lot for your help Ned
Looking at it now, its crazy how that took me more than a day.
Summary below (also with new locations for installations).
Building sqlite 3.40
Build/install:
cd ~/b
git clone git@github.com:sqlite/sqlite.git
cd sqlite
git checkout origin/branch-3.40
mkdir build-3.40; cd build-3.40
../configure --prefix=/opt/sqlite-3.40
make
sudo make install
Test:
$ /opt/sqlite-3.40/bin/sqlite3 --version
3.40.2 2023-01-14 19:36:47 c66c7734feceb6ee9ae079df4fa4d078891d9e5fe6a44a2acfc482598a7176b8
Building python with sqlite 3.40
Build/install:
cd ~/b
git clone git@github.com:python/cpython.git
git checkout origin/3.13
cd cpython
mkdir build-custom; cd build-custom
../configure \
--prefix=/opt/python-3.13 \
LIBSQLITE3_CFLAGS=-I~/sqlite-3.40/include \
LIBSQLITE3_LIBS="-L~/sqlite-3.40/lib -lsqlite3"
make
sudo make install
Test
Remember to set LD_LIBRARY_PATH !!
$ /opt/python-3.13/bin/python3 -c "import sqlite3; print(sqlite3.sqlite_version)"
3.37.2
$ LD_LIBRARY_PATH=/opt/sqlite-3.40/lib /opt/python-3.13/bin/python3 -c "import sqlite3; print(sqlite3.sqlite_version)"
4.40.2