SQLite UUID extension wrapper
Easy way to install the SQLite loadable extension in a Python environment.
Installation
pip install sqlite-uuid
Notes
When you install this package, an SQLite loadable extension will get built. In Python, you can load extensions in 2 ways. The first step is enabling the loadable extensions using conn.enable_load_extension(True). Then you can use conn.load_extension(path). However, there's a problem with this. The SQLite UUID extension has a name that conflicts with a Python module from the standard library that has the same name. To avoid confusion, this module is built with the filename sqlite_uuid_ext.[py-ver].[extension].
When you try to load an extension in SQLite, it needs an entrypoint function. According to the docs of the sqlite3_load_extension C function, if an entrypoint is not provided, it will try to guess one base on the filename. In this case it will try to load an entrypoint called sqlite3_sqlite_uuid_ext_init, because the file has the name sqlite_uuid_ext. However, the entrypoint in this extension has the name sqlite3_uuid_init, so it won't work. The good news is that there's also an SQL function to load extensions, and it lets you specify the entrypoint. With that we can do:
conn.execute("select load_extension('path/to/loadable/extension/sqlite_uuid_ext.[py-ver].[extension]', 'sqlite3_uuid_init')")
The first option is the path to our compiled extension, the second one is the entrypoint function.
SQLite version
This extension uses the SQLITE_INNOCUOUS flag.
The SQLITE_INNOCUOUS flag is a new feature for SQLite version 3.31.0. Make sure you have at least that version installed, although you may be able to get around it.
Examples
import sqlite3
import sqlite_uuid
from uuid import UUID
# create an in-memory DB
conn = sqlite3.connect(":memory:")
# enable loadable extensions
conn.enable_load_extension(True)
# load UUID extension as explained above
conn.execute(
"select load_extension(:path, 'sqlite3_uuid_init')",
{"path": sqlite_uuid.extension_path()},
)
# disable loadable extensions
conn.enable_load_extension(False)
print("OK")
# create a UUID (as a string)
res = conn.execute("SELECT uuid()").fetchall()
print(res)
# convert it to a BLOB (bytes) and return it
res = conn.execute("SELECT uuid_blob(uuid())").fetchall()
# get the returned result
bx = res[0][0]
# build python UUID object from the bytes
u = UUID(bytes=bx, version=4)
# make sure they are the same
assert u.bytes == bx
assert u.hex == bx.hex()
exit()
(the script above should run correctly as it's written)
Release History
- 0.9 * Initial release
Meta
Ricardo Ander-Egg Aguilar – @ricardoanderegg –
Distributed under the MIT license. See LICENSE for more information.
Credits
I initially took the repository karlb/sqlite-spellfix as a template to create this Python package.
Metadata
Release files for sqlite-uuid 0.9.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sqlite-uuid-0.9.1.tar.gz | 5.7 kB | Without provenance | Details for sqlite-uuid-0.9.1.tar.gz |
Release files / sqlite-uuid-0.9.1.tar.gz
| Download URL | sqlite-uuid-0.9.1.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f024fe853204be598e6fc5e4440e716bf1e6d1f8557db2b020d9c53aae69326c
|
|
BLAKE2b-256 checksum How to use checksums |
b1552b8a77139dffaef1adb579fa4f0606c9e1b6335db05254690560ce271455
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
|
Security for sqlite-uuid 0.9.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.