I made it because I wanted it when reading JSON with sqlite3. Make a note so as not to forget
Use sqlite3.register_adapter
to use Python types in SQLite.
Conversely, to convert from SQlite to Python, use sqlite3.register_conveter
.
import sqlite3
#User-defined type 1
List = list
sqlite3.register_adapter(List, lambda l: ';'.join([str(i) for i in l]))
sqlite3.register_converter('IntList', lambda s: [str(i) for i in s.split(bytes(b';'))])
#User-defined type part 2
Bool = bool
sqlite3.register_adapter(Bool, lambda b: str(b))
sqlite3.register_converter('Bool', lambda l: bool(eval(l)))
(Py2 version) https://qiita.com/maueki/items/4aae7b2d9a34758ef465 (Official Docs) https://docs.python.org/ja/3/library/sqlite3.html#using-adapters-to-store-additional-python-types-in-sqlite-databases
Recommended Posts