sqlite3.register_converter
Prev: sqlite3.register_adapter | Up: sqlite3 | Next: sqlite3.Row
Rel: sqlite3.adapt, sqlite3.Binary, sqlite3.Blob, sqlite3.complete_statement
Ref: docs.python.org — sqlite3.register_converter
Tags: #public #python3 #stdlib #sqlite3
Import
import sqlite3
from sqlite3 import register_converter
Signature & Overview
sqlite3.register_converter(typename, converter, /)
Register a function to convert SQLite values to Python objects.
Usage
import sqlite3
sqlite3.register_converter("CSVLIST", lambda b: b.decode("utf-8").split(","))
con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
con.execute("CREATE TABLE t (tags CSVLIST)")
con.execute("INSERT INTO t VALUES (?)", ("a,b,c",))
assert con.execute("SELECT tags FROM t").fetchone() == (["a", "b", "c"],)
con.close()
Gotchas
- Requires
detect_types=sqlite3.PARSE_DECLTYPES(orPARSE_COLNAMES): Converters are ignored unlessdetect_typesis passed tosqlite3.connect(). - Converters receive
bytes: Decode UTF-8 explicitly inside your converter function.
REPL Inspect
from sqlite3 import register_converter
help(register_converter)
Linked from
Related notes
- tuts/python3/stdlib_module/sqlite3 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/register_adapter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/Row Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/adapt Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/Binary Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/Blob Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/complete_statement Linked here; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib