import sqlite3 sqlite3.register_adapter(type, callable)
type
- значение Python,callable
- функция преобразования.Функция register_adapter()
модуля sqlite3
регистрирует вызываемый объект (функцию) callable
для преобразования пользовательского типа Python в один из поддерживаемых типов SQLite.
Вызываемый объект callable
принимает в качестве единственного параметра type
- значение Python и должен возвращать значения следующих типов: int
, float
, str
или bytes
.
Дополнительно смотрите разделы:
import sqlite3 class Point: def __init__(self, x, y): self.x, self.y = x, y def adapt_point(point): return "%f;%f" % (point.x, point.y) sqlite3.register_adapter(Point, adapt_point) con = sqlite3.connect(":memory:") cur = con.cursor() p = Point(4.0, -3.2) cur.execute("select ?", (p,)) print(cur.fetchone()[0]) con.close()