Implemented List and Bool in Python and SQLite3 (personal note)

Preface

I made it because I wanted it when reading JSON with sqlite3. Make a note so as not to forget

Implementation

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)))

References

(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

Implemented List and Bool in Python and SQLite3 (personal note)
Foreign Key in Python SQLite [Note]
Difference between append and + = in Python list
Sqlite in python
Implemented memoization recursion and exploration in Python and Go
Sorted list in Python
Implemented SimRank in Python
Filter List in Python
List find in Python
Implemented Shiritori in Python
Mayungo's Python Learning Note: List of stories and links
Extract and list personal names and place names in the text
Things to note when initializing a list in Python
[Python] How to sort dict in list and instance in list
Python Input Note in AtCoder
Stack and Queue in Python
Sudoku solver implemented in Python 3
Python list and tuples and commas
Python list comprehensions and generators
[AtCoder] ABC165C Personal Note [Python]
Unittest and CI in Python
Getting list elements in Python
6 Ball puzzle implemented in python
List method argument information for classes and modules in Python
Sort and output the elements in the list as elements and multiples in Python.
List concatenation method in python, difference between list.extend () and “+” operator
Poisson distribution and Poisson cumulative distribution plot via sqlite in Python and Java
Useful tricks related to list and for statements in Python
Extract multiple list duplicates in Python
MIDI packages in Python midi and pretty_midi
[python] Manage functions in a list
Output 2017 Premium Friday list in Python
Difference between == and is in python
Implemented image segmentation in python (Union-Find)
View photos in Python and html
python Note: Determine if command line arguments are in the list
Sorting algorithm and implementation in Python
How to use SQLite in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Widrow-Hoff learning rules implemented in Python
Check and move directories in Python
Implemented label propagation method in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Implemented Perceptron learning rules in Python
Export and output files in Python
List of Linear Programming (LP) solvers and modelers available in Python
(Personal notes) Python metaclasses and metaprogramming
Reverse Hiragana and Katakana in Python2.7
Delete multiple elements in python list
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Implemented in 1 minute! LINE Notify in Python
Create and read messagepacks in Python
Reading from text files and SQLite in Python (+ Pandas), R, Julia (+ DataFrames)
Extract every n elements from an array (list) in Python and Ruby
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript