[PYTHON] Use sqlite load_extensions with Pyramid

In SQLite, it is necessary to use an extension library to use mathematical functions and statistical functions, but it was a little devised to use it from Pyramid.

Introduction of expansion

Download extension-functions.c from the following URL.

gcc -fPIC -shared /home/takaki/Desktop/extension-functions.c -o libsqlitefunctions.so

If you can start sqlite and do the following, the extension library is OK.

$ sqlite3 
SQLite version 3.13.0 2016-05-18 10:57:30
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> select load_extension('./libsqlitefunctions.so');

sqlite> select sin(3.14/2);
0.999999682931835
sqlite> 

Call from Pyramid

Make the following changes from the normal template file.

model/__init__.py


def get_session_factory(engine):
    @event.listens_for(engine, "connect")
    def connect(dbapi_connection, connection_rec):
        dbapi_connection.enable_load_extension(True)
        dbapi_connection.execute("SELECT load_extension('{0}');".format(FULL_PATH_OF_LIB))
    factory = sessionmaker()
    factory.configure(bind=engine)
    return factory

Recommended Posts

Use sqlite load_extensions with Pyramid
Redirect with Pyramid
Play with Pyramid
Use mecab-ipadic-neologd with igo-python
Use ansible with cygwin
Use pipdeptree with virtualenv
[Python] Use JSON with Python
Use Mock with pytest
Use indicator with pd.merge
Use Gentelella with django
Use mecab with Python3
Use tensorboard with Chainer
Use DynamoDB with Python
Use pip with MSYS2
Use Python 3.8 with Anaconda
Use pyright with Spacemacs
Use python with docker
Use TypeScript with django-compressor
Use LESS with Django
Easy to use SQLite3
Use MySQL with Django
Use Enums with SQLAlchemy
Use tensorboard with NNabla
Use GPS with Edison
Use nim with Jupyter
Use Cursur that closes automatically with sqlite3 in Python
Use Trello API with python
Use shared memory with shared libraries
Use custom tags with PyYAML
Use directional graphs with networkx
Use TensorFlow with Intellij IDEA
Use Twitter API with Python
Use pip with Jupyter Notebook
Use DATE_FORMAT with SQLAlchemy filter
Use TUN / TAP with Python
Use Windows 10 fonts with WSL
Use chainer with Jetson TK1
Use SSL with Celery + Redis
Use Cython with Jupyter Notebook
Use Maxout + CNN with Pylearn2
Use WDC-433SU2M2 with Manjaro Linux
Use OpenBLAS with numpy, scipy
[Python] [SQLite3] Operate SQLite with Python (Basic)
Use subsonic API with python3
Use Sonicwall NetExtener with Systemd
Use prefetch_related conveniently with Django
Use AWS interpreter with Pycharm
Use Bokeh with IPython Notebook
Use Python-like range with Rust
Use MLflow with Databricks ④ --Call model -
When coverage fails with _sqlite3 error
Use pyright with CentOS7, emacs lsp-mode
Python: How to use async with
Use Azure SQL Database with SQLAlchemy
Use PointGrey camera with Python (PyCapture2)
Use PX-S1UD / PX-Q1UD with Jetson nano
Use the preview feature with aws-cli
Easily handle lists with python + sqlite3
How to use SQLite in Python
How to use virtualenv with PowerShell
[Python] Use Basic/Digest authentication with Flask