| 1 | {{{ |
| 2 | #!text/x-rst |
| 3 | ================================== |
| 4 | Enabling JSON1 extension on SQLite |
| 5 | ================================== |
| 6 | |
| 7 | To use **django.db.models.JSONField** on SQLite, you need to enable the `JSON1 extension`__ on Python's `sqlite3`__ library. If the extension is not enabled on your installation, a system error (`fields.E180`) will be raised. To check if the extension is enabled on your installation, you can do a query with one of the functions included in the extension, e.g. `JSON()`. For example:: |
| 8 | |
| 9 | >>> import sqlite3 |
| 10 | >>> conn = sqlite3.connect(':memory:') |
| 11 | >>> cursor = conn.cursor() |
| 12 | >>> cursor.execute('SELECT JSON(\'{"a": "b"}\')') |
| 13 | |
| 14 | If the query doesn't throw any errors, then the JSON1 extension is already enabled. Otherwise, follow the instructions below according to your operating system to set it up correctly. |
| 15 | |
| 16 | .. __: https://www.sqlite.org/json1.html |
| 17 | .. __: https://docs.python.org/3/library/sqlite3.html#module-sqlite3 |
| 18 | |
| 19 | Linux |
| 20 | ===== |
| 21 | |
| 22 | On most major Linux distributions, the JSON1 extension is included in their SQLite and/or Python packages and enabled by default. If that's not the case on your installation, then do the following: |
| 23 | |
| 24 | - Download the `SQLite amalgamation`__, with or without the configuration script. |
| 25 | - Extract the source code archive and enter the directory of the result. |
| 26 | - Compile the source code using the `-DSQLITE_ENABLE_JSON1` flag to enable the JSON1 extension. For example:: |
| 27 | |
| 28 | gcc -DSQLITE_ENABLE_JSON1 -c -fPIC sqlite3.c |
| 29 | |
| 30 | To enable other extensions, see the `compilation instructions`__. |
| 31 | - Create a shared library. For example:: |
| 32 | |
| 33 | gcc -shared -o libsqlite3.so -fPIC sqlite3.o -ldl -lpthread |
| 34 | |
| 35 | - Place the resulting file (`libsqlite3.so`) in a desired directory, e.g. `/usr/lib/sqlite3/`. |
| 36 | - Set the `LD_PRELOAD` environment variable to use your compiled SQLite every time you run Django. For example:: |
| 37 | |
| 38 | export LD_PRELOAD=/usr/lib/sqlite3/libsqlite3.so |
| 39 | |
| 40 | - Now, the JSON1 extension should be ready to be used in Python and Django. |
| 41 | |
| 42 | .. __: https://www.sqlite.org/download.html |
| 43 | .. __: https://www.sqlite.org/howtocompile.html |
| 44 | |
| 45 | macOS |
| 46 | ===== |
| 47 | |
| 48 | On macOS, follow the instructions for Linux above, but instead of setting the `LD_PRELOAD` environment variable, use `DYLD_LIBRARY_PATH`. For example:: |
| 49 | |
| 50 | export DYLD_LIBRARY_PATH=/usr/lib/sqlite3 |
| 51 | |
| 52 | Windows |
| 53 | ======= |
| 54 | |
| 55 | - Download the `precompiled DLL`__ that matches your Python installation (32-bit or 64-bit). |
| 56 | - Locate your Python installation. By default, it should be in `%localappdata%\Programs\Python\PythonXX`, where `XX` is the Python version. For example, it's located in `C:\Users\<username>\AppData\Local\Programs\Python\Python37`. If you added Python installation directory to your ``PATH`` environment variable, you can run the command `where python` on a command prompt to locate it. |
| 57 | - Enter the `DLLs` directory in your Python installation. |
| 58 | - Rename (or delete) `sqlite3.dll` inside the `DLLs` directory. |
| 59 | - Extract `sqlite3.dll` from the downloaded DLL archive and put it in the `DLLs` directory. |
| 60 | - Now, the JSON1 extension should be ready to be used in Python and Django. |
| 61 | |
| 62 | .. __: https://www.sqlite.org/download.html |
| 63 | |
| 64 | }}} |