all Python 3sqlite3function
sqlite3.adapt()
adapters and converters
An undocumented function: it applies the registered adapter or the object's __conform__ method — the same step sqlite3 takes for a query parameter.
Syntax
sqlite3.adapt(obj, proto=sqlite3.PrepareProtocol, alt=<не задан>)
Handy for testing an adapter without touching a database. The catch: native types do not pass through — adapt(1) with no adapter raises ProgrammingError "can't adapt", even though 1 binds fine in a query; with a third argument alt you get alt instead of the error. An adapter from register_adapter wins over __conform__. It is not in the docs, so production code should not rely on it.
Examples
import sqlite3
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
sqlite3.register_adapter(Point, lambda p: f'{p.x};{p.y}')
print(sqlite3.adapt(Point(1, 2))) # → 1;2
print(sqlite3.adapt(1, sqlite3.PrepareProtocol, 'нет адаптера')) # → нет адаптера
try:
sqlite3.adapt(1)
except sqlite3.ProgrammingError as e:
print(e) # → can't adapt
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy