[PYTHON] How to search by string to use mysql json_contains in SQLAlchemy

I was a little addicted when I tried to search by string using json_contains.

Success story

Must be enclosed in " ". In fact, you need to use json_contains (fuga,'"piyo"') when you select directly with mysql.

success.py


from sqlalchemy import func

search_str = 'piyo'
db.session.query(Hoge).filter(func.json_contains(Hoge.fuga, f'"{search_str}"'))

Failure code example

fail.py


from sqlalchemy import func

search_str = 'piyo'
db.session.query(Hoge).filter(func.json_contains(Hoge.fuga, search_str))

Error message

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (3141, 'Invalid JSON text in argument 2 to function json_contains: "Invalid value." at position 0.')

Recommended Posts

How to search by string to use mysql json_contains in SQLAlchemy
How to use Mysql in python
How to use search sorted
How to handle session in SQLAlchemy
How to use SQLite in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to use bing search api
How to use calculated columns in CASTable
How to use Google Test in C
How to use Anaconda interpreter in PyCharm
How to handle consecutive values in MySQL
How to use __slots__ in Python class
How to use Map in Android ViewPager
How to use is and == in Python
How to use SQLAlchemy / Connect with aiomysql
How to use the C library in Python
How to embed a variable in a python string
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
How to use tkinter with python in pyenv
How to use loc / iloc / ix to get by specifying a column in CASTable
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
[For beginners] How to use say command in python!
How to convert / restore a string with [] in python
How to write string concatenation in multiple lines in Python
[Python] How to expand variables in a character string
A memorandum on how to use keras.preprocessing.image in Keras
How to use bootstrap in Django generic class view
How to use template engine in pyramid 1 file application
How to use the exists clause in Django's queryset
I tried to summarize how to use pandas in python