mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
@db.register_function decorator, closes #162
This commit is contained in:
parent
ef882986d0
commit
482477585a
3 changed files with 44 additions and 0 deletions
|
|
@ -1415,3 +1415,25 @@ You can use it in code like this:
|
|||
if spatialite:
|
||||
db.conn.enable_load_extension(True)
|
||||
db.conn.load_extension(spatialite)
|
||||
|
||||
.. _python_api_register_function:
|
||||
|
||||
Registering custom SQL functions
|
||||
================================
|
||||
|
||||
SQLite supports registering custom SQL functions written in Python. The ``@db.register_function`` decorator provides syntactic sugar for registering these functions.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from sqlite_utils import Database
|
||||
|
||||
db = Database(memory=True)
|
||||
|
||||
@db.register_function
|
||||
def reverse_string(s):
|
||||
return "".join(reversed(list(s)))
|
||||
|
||||
print(db.execute('select reverse_string("hello")').fetchone())[0]
|
||||
# This prints "olleh"
|
||||
|
||||
If your Python function accepts multiple arguments the SQL version will accept the same number of arguments.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue