python读写mysql的json字段内容

2022年8月13日 19:32 by wst

python高级

困惑

MySQL数据库是我们用的最多的数据库,但是有个头疼的地方:不能读写json数据。

新特性

自从MySQL5.7之后,开始支持json数据的读写了,这是值得欢呼的好事!

json数据还可以搜索、根据path获取等等特性,详见这篇文章

实例

今天以一个例子来阐述json字段的读写。

目的:对于不常变化的数据,可以采用json,这里为用户信息。(逻辑不太自洽,但用法可行)如下图所示:

代码如下:

#!/usr/bin/env python

"""
FileName: test_commodity
Author: wst
Email: movingheart000@gmail.com
Date: 2022/8/4 09:26:54
Desc: 
"""
import asyncio
import json

from lib.pub_mysql import query_mysql, insert_mysql, pymysql
from lib.pub import mysql_pool


async def write_json():
    """ 存储json数据到mysql"""
    dic = {
        "name": "李四",
        "age": 22
    }
    sql = f"insert into UserTag(userTags) values ('{pymysql.escape_string(json.dumps(dic))}')"
    res = await insert_mysql(mysql_pool, sql)
    print(res)
    return res


async def read_json():
    """ 读取json数据 """
    sql = f"select * from UserTag where userId=5"
    res = await query_mysql(mysql_pool, sql)
    print("res:", res)
    return res


if __name__ == "__main__":
    # asyncio.get_event_loop().run_until_complete(write_json())
    asyncio.get_event_loop().run_until_complete(read_json())

说明:

1. 截图里的数据,userId为1、2的记录为手动修改数据库实现的。

2. userId为3的记录,UserTags内容未包含汉字,可正常显示。

3. userId为4的记录,插入的时候未使用pymysql.escape_string处理,汉字不能正常显示。

4. userId为5的记录,使用了pymysql.escape_string处理,汉字可以正常显示,用代码读写都没有问题。

 

如有疑问,欢迎探讨。v:Beeb2b3


Comments(126) Add Your Comment

Xcssgo
how to buy lipitor <a href="https://lipiws.top/">order lipitor 20mg</a> atorvastatin pills
Etgyev
cipro without prescription - <a href="https://cipropro.top/bactrimos/">where to buy bactrim without a prescription</a> clavulanate where to buy
Ggowxs
buy cipro - <a href="https://metroagyl.top/keflexcapsule/">order cephalexin 500mg sale</a> order augmentin online
Toklns
order ciplox 500 mg pills - <a href="https://septrim.top/doryx/">cheap doxycycline generic</a> generic erythromycin 500mg
Nayqgz
order flagyl - <a href="https://metroagyl.top/">metronidazole 400mg uk</a> buy zithromax 500mg sale
Zbloxr
stromectol online pharmacy - <a href="https://keflexin.top/sumycin/">sumycin generic</a> tetracycline sale
Bxmmym
buy valtrex 1000mg sale - <a href="https://gnantiviralp.com/nemasole/">nemasole tablets</a> acyclovir us
Pjdcmu
buy generic acillin over the counter <a href="https://ampiacil.top/">purchase ampicillin generic</a> buy amoxil without a prescription
Hkronp
order generic metronidazole - <a href="https://metroagyl.top/azithromycin/">zithromax price</a> order zithromax 250mg generic
Irdezc
lasix pills - <a href="https://antipathogenc.com/capoten/">buy cheap captopril</a> capoten 25 mg cost
Jhdnwh
glucophage pill - <a href="https://gnantiacidity.com/baycip/">buy cipro 500mg sale</a> generic lincomycin 500mg
Ecbjur
order zidovudine sale - <a href="https://canadiangnp.com/epivir/">buy epivir medication</a> buy cheap zyloprim
Lmqawz
buy clozapine 50mg generic - <a href="https://genonlinep.com/accupril/">buy accupril medication</a> order famotidine 40mg for sale
Pmgohl
generic quetiapine 100mg - <a href="https://gnkantdepres.com/luvox/">fluvoxamine 50mg drug</a> buy generic eskalith over the counter
Iuddfk
purchase anafranil pill - <a href="https://antdeponline.com/">clomipramine drug</a> doxepin 25mg price
Pikcvz
atarax medication - <a href="https://antdepls.com/prozac/">buy generic prozac 20mg</a> amitriptyline 25mg usa
Rbqtrk
buy augmentin 1000mg without prescription - <a href="https://atbioinfo.com/">amoxiclav oral</a> order generic baycip
Siyhmr
where can i buy amoxil - <a href="https://atbioxotc.com/cephalexin/">buy generic keflex online</a> cost cipro 1000mg
Wmpybu
buy zithromax pills - <a href="https://gncatbp.com/">buy azithromycin 500mg</a> ciplox 500 mg pills
Irtpbd
buy cleocin 300mg for sale - <a href="https://cadbiot.com/">cleocin 150mg price</a> generic chloramphenicol
Nmahzd
ivermectin pills - <a href="https://antibpl.com/ascefaclor/">buy generic cefaclor online</a> order cefaclor 500mg generic
Sberuf
ventolin inhalator order online - <a href="https://antxallergic.com/advairdiskus/">purchase advair diskus inhalator online</a> buy generic theophylline over the counter
Uuhart
buy medrol online cheap - <a href="https://ntallegpl.com/loratadine/">order loratadine pill</a> order azelastine 10 ml without prescription
Eteqyv
buy desloratadine 5mg for sale - <a href="https://rxtallerg.com/beclomethasone/">beclomethasone buy online</a> best antihistamine pills
Batcag
purchase glyburide generic - <a href="https://prodeprpl.com/dapagliflozin5/">dapagliflozin for sale</a> buy forxiga paypal
Nfvyre
buy generic metformin 1000mg - <a href="https://arxdepress.com/losartan25/">losartan 50mg sale</a> buy acarbose 25mg online
Lgokkk
buy prandin 2mg for sale - <a href="https://depressinfo.com/">buy repaglinide 1mg generic</a> empagliflozin us
Gyvlwq
semaglutide 14 mg usa - <a href="https://infodeppl.com/">order semaglutide 14mg generic</a> purchase desmopressin for sale
Vtxxdl
order terbinafine 250mg pill - <a href="https://treatfungusx.com/xgrifulvinvs/">buy griseofulvin sale</a> buy griseofulvin without prescription
Aaohoh
generic ketoconazole - <a href="https://antifungusrp.com/clotrithasone/">order lotrisone generic</a> order sporanox 100mg online cheap
Kyflqh
order famvir 500mg online - <a href="https://amvinherpes.com/acyclovircream/">order zovirax 800mg pill</a> valaciclovir drug
Zkbrtr
digoxin 250 mg cheap - <a href="https://blpressureok.com/dipyridamole100/">purchase dipyridamole pill</a> generic furosemide 40mg
Ilgmtf
buy lopressor 100mg without prescription - <a href="https://bloodpresspl.com/olmesartan/">benicar 10mg tablet</a> buy generic adalat 30mg
Oipaou
buy cheap microzide - <a href="https://norvapril.com/bisoprolol/">buy bisoprolol generic</a> order bisoprolol 10mg online
Wmvzdz
nitroglycerin over the counter - <a href="https://nitroproxl.com/">purchase nitroglycerin generic</a> valsartan 160mg tablet
Siwobs
simvastatin trickle - <a href="https://canescholest.com/gemfibrozil/">gemfibrozil ourself</a> atorvastatin abroad
Mczuxl
crestor pills move - <a href="https://antcholesterol.com/pravastatin/">pravastatin buy pass</a> caduet online fright
Utrfjo
viagra professional call - <a href="https://edsildps.com/superkamagra/">super kamagra bomb</a> levitra oral jelly online wizard
Pbjkxq
dapoxetine soak - <a href="https://promedprili.com/viagraplus/">viagra plus rage</a> cialis with dapoxetine town
Rksthf
cenforce online potter - <a href="https://xcenforcem.com/levitraprofessional/">levitra professional bargain</a> brand viagra explanation
Oulemz
brand cialis somewhat - <a href="https://probrandtad.com/viagrasofttabs/">viagra soft tabs arise</a> penisole peak
Lofbot
brand cialis cause - <a href="https://probrandtad.com/alprostadilonline/">alprostadil chair</a> penisole russian
Qoabhl
cialis soft tabs pills fresh - <a href="https://supervalip.com/tadarisepills/">tadarise midst</a> viagra oral jelly which
Ppeziz
cialis soft tabs pills assure - <a href="https://supervalip.com/">cialis soft tabs pills pitch</a> viagra oral jelly damage
Cehntc
dapoxetine arrange - <a href="https://promedprili.com/zudena/">zudena morning</a> cialis with dapoxetine often
Bxfaxn
cenforce online delightful - <a href="https://xcenforcem.com/levitraprofessional/">levitra professional pills fence</a> brand viagra pills warrior
Kbmmub
asthma treatment ourself - <a href="https://bsasthmaps.com/">asthma medication forbid</a> asthma treatment trace
Dgqoao
acne medication know - <a href="https://placnemedx.com/">acne medication native</a> acne treatment request
Suvpsd
prostatitis pills clear - <a href="https://xprosttreat.com/">prostatitis pills serpent</a> prostatitis pills sore
Kmjuux
uti medication equipment - <a href="https://amenahealthp.com/">uti antibiotics pity</a> uti medication receive
Sfguxu
claritin pills chocolate - <a href="https://clatadine.top/">claritin scrooge</a> loratadine medication escape
Rvtetz
valacyclovir online shrill - <a href="https://gnantiviralp.com/">valtrex pills nanny</a> valtrex try
Pxjcbl
dapoxetine passionate - <a href="https://prilixgn.top/">priligy trace</a> priligy unhappy
Ovavou
loratadine ought - <a href="https://clatadine.top/">claritin pills forbid</a> loratadine year
Xxzwmn
ascorbic acid important - <a href="https://ascxacid.com/">ascorbic acid graceful</a> ascorbic acid coal
Ttybha
promethazine nameless - <a href="https://prohnrg.com/">promethazine likewise</a> promethazine pursue
Icmmul
clarithromycin pills slam - <a href="https://gastropls.com/albendazole400/">albendazole appeal</a> cytotec pills twin
Aoieus
florinef pills spectacle - <a href="https://gastroplusp.com/prlansoprazole/">prevacid pills useless</a> prevacid hammer
Tikase
aciphex 20mg canada - <a href="https://gastrointesl.com/domperidone/">buy motilium 10mg pills</a> motilium 10mg canada
Rxnzgt
bisacodyl 5mg usa - <a href="https://gastroinfop.com/liv52/">liv52 20mg for sale</a> order liv52 for sale
Ocjtmg
purchase eukroma for sale - <a href="https://dupsteron.com/">purchase duphaston online</a> buy dydrogesterone medication
Wqytjl
buy bactrim without prescription - <a href="https://tobmycin.com/">tobra oral</a> order generic tobrex 10mg
Tfwnvl
fulvicin 250 mg usa - <a href="https://fulviseoful.com/">buy generic griseofulvin</a> order gemfibrozil 300 mg for sale
Vxlwfc
forxiga usa - <a href="https://sineqpin.com/">order doxepin 75mg without prescription</a> order precose generic
Hsdjii
order dramamine 50 mg pills - <a href="https://bisacolax.com/">buy dimenhydrinate 50 mg pills</a> purchase actonel online
Pmudis
enalapril 5mg us - <a href="https://vasolapril.com/">order enalapril 5mg generic</a> order latanoprost
Jwupan
buy etodolac for sale - <a href="https://etodograph.com/">order monograph 600 mg generic</a> cilostazol 100mg brand
Tkwapu
feldene 20mg oral - <a href="https://rivastilons.com/">rivastigmine for sale online</a> buy generic rivastigmine 3mg
Eobrww
oral piracetam 800mg - <a href="https://nootquin.com/praziquantel/">praziquantel 600 mg without prescription</a> order sinemet pill
Gjgzlf
buy hydrea generic - <a href="https://hydroydrinfo.com/trecatorsc/">buy ethionamide no prescription</a> methocarbamol uk
Nafbfj
purchase divalproex sale - <a href="https://adepamox.com/aspirindipyridamole/">aggrenox pills</a> cost topamax 100mg
Ujtjjd
norpace pill - <a href="https://anorpica.com/pregabal/">order pregabalin 150mg without prescription</a> cost thorazine 100mg
Fymcpm
spironolactone 25mg sale - <a href="https://aldantinep.com/dipyridamole/">buy dipyridamole tablets</a> buy generic revia over the counter
Whxvxo
cytoxan tablet - <a href="https://cycloxalp.com/">buy generic cyclophosphamide over the counter</a> vastarel price
Gmkbjb
order cyclobenzaprine pill - <a href="https://abflequine.com/">oral cyclobenzaprine</a> vasotec 5mg generic
Mmmkje
generic zofran - <a href="https://azofarininfo.com/ropinirole/">ropinirole 1mg ca</a> cost requip 1mg
Ieduyy
generic ascorbic acid 500 mg - <a href="https://mdacidinfo.com/bromhexine/">buy bromhexine for sale</a> buy compro medication
Nwyetn
order durex gel sale - <a href="https://xalaplinfo.com/durexcondoms/">order durex condoms</a> buy generic zovirax
Iqcqma
buy rogaine cheap - <a href="https://hairlossmedinfo.com/">where to buy rogaine without a prescription</a> proscar 5mg uk
Fcvhrk
order leflunomide 10mg online - <a href="https://infohealthybones.com/alfacalcidol/">alfacip over the counter</a> buy cartidin medication
Nnzcuj
calan medication - <a href="https://infoheartdisea.com/atenolol/">tenoretic cheap</a> buy tenoretic
Aqslwa
cost atenolol 50mg - <a href="https://heartmedinfox.com/clopidogrel/">buy plavix 150mg generic</a> carvedilol buy online
Oinosd
atorlip order - <a href="https://infoxheartmed.com/">cheap atorvastatin for sale</a> brand nebivolol
Dxqzvd
gasex price - <a href="https://herbalinfomez.com/ashwagandha/">ashwagandha usa</a> diabecon pills
Ppebgz
buy lasuna without a prescription - <a href="https://infoherbalmz.com/himcolin/">himcolin canada</a> purchase himcolin generic
Ehaihv
cheap noroxin pills - <a href="https://gmenshth.com/confido/">cheap confido online</a> buy confido pills
Dhggjt
cheap finasteride pills - <a href="https://finmenura.com/doxazosin/">cheap cardura</a> buy alfuzosin 10mg pills
Hpwlca
terazosin price - <a href="https://hymenmax.com/tamsulosin/">flomax 0.2mg pills</a> buy dapoxetine 90mg generic
Rzebky
speman online order - <a href="https://spmensht.com/himplasia/">himplasia usa</a> finasteride over the counter
Haqfcd
oxcarbazepine 300mg pill - <a href="https://trileoxine.com/">oxcarbazepine pill</a> cheap levoxyl online
Gqkpxf
cheap imusporin - <a href="https://asimusxate.com/">imusporin tubes</a> order colcrys 0.5mg for sale
Rpqpdh
buy duphalac cheap - <a href="https://duphalinfo.com/">buy lactulose bottles for sale</a> betahistine usa
Fooiok
buy deflazacort pill - <a href="https://lazacort.com/brimonidine/">buy brimonidine cheap</a> alphagan oral
Xdbumt
cheap besifloxacin - <a href="https://besifxcist.com/">buy generic besifloxacin for sale</a> buy sildamax without a prescription
Rzsuka
brand gabapentin 800mg - <a href="https://aneutrin.com/sulfasalazine/">buy generic azulfidine 500 mg</a> sulfasalazine buy online
Mdtdxt
probenecid 500 mg cost - <a href="https://bendoltol.com/">probalan medication</a> buy carbamazepine sale
Vwrqkd
order celecoxib pill - <a href="https://celespas.com/">buy celecoxib generic</a> buy indocin 75mg generic
Qhstpi
purchase mebeverine pills - <a href="https://coloxia.com/">buy mebeverine 135 mg online cheap</a> how to get cilostazol without a prescription
Knmjpb
buy generic diclofenac for sale - <a href="https://dicloltarin.com/aspirin/">buy aspirin 75mg online cheap</a> order aspirin sale
Vvedds
cheap rumalaya generic - <a href="https://rumaxtol.com/amitriptyline/">order elavil 10mg online</a> buy amitriptyline 50mg for sale
Vzchuc
brand mestinon 60 mg - <a href="https://mestonsx.com/">buy pyridostigmine 60mg online</a> cost imuran
Ikwuqm
voveran pills - <a href="https://vovetosa.com/">brand diclofenac</a> cheap nimotop pill
Jphmcx
buy ozobax for sale - <a href="https://baclion.com/nspiroxicam/">feldene 20mg without prescription</a> purchase piroxicam
Vvwhqk
buy generic mobic - <a href="https://meloxiptan.com/">how to buy mobic</a> toradol over the counter
Xwlpeg
cyproheptadine 4 mg sale - <a href="https://periheptadn.com/">periactin online buy</a> buy tizanidine 2mg sale
Zdjtfy
purchase artane generic - <a href="https://voltapll.com.com/">trihexyphenidyl order online</a> buy diclofenac gel for sale
Fusbiv
order generic absorica - <a href="https://aisotane.com/">order isotretinoin pill</a> buy deltasone 40mg sale
Xsgzqs
brand prednisone 40mg - <a href="https://apreplson.com/">prednisone 20mg cheap</a> buy permethrin generic
Mihzfp
acticin without prescription - <a href="https://actizacs.com/asbenzac/">buy generic benzoyl peroxide for sale</a> buy tretinoin gel online cheap
Xhbstg
buy betnovate 20gm online - <a href="https://betnoson.com/awdifferin/">buy adapalene cream for sale</a> buy monobenzone without prescription
Ggewtq
flagyl 400mg canada - <a href="https://ametronles.com/">flagyl for sale online</a> cenforce online order
Emifsp
order amoxiclav without prescription - <a href="https://alevonted.com/">synthroid 100mcg generic</a> synthroid usa
Vdhoun
cleocin 150mg oral - <a href="https://indometnp.com/">order indomethacin 50mg for sale</a> indomethacin 50mg for sale
Wqcejz
order hyzaar online cheap - <a href="https://acephacrsa.com/">cephalexin 250mg oral</a> purchase keflex generic
Mzznkb
how to buy eurax - <a href="https://aeuracream.com/">buy eurax without prescription</a> buy aczone for sale
Ilykoh
buy provigil for sale - <a href="https://sleepagil.com/">provigil 200mg price</a> melatonin 3 mg pills
Aumryl
order zyban 150mg without prescription - <a href="https://bupropsl.com/orlistat/">oral orlistat</a> purchase shuddha guggulu pills
Ggpgxv
buy progesterone generic - <a href="https://apromid.com/clomipheneferto/">buy fertomid online</a> fertomid online buy
Cgrqnm
order generic xeloda - <a href="https://xelocap.com/">where can i buy capecitabine</a> danazol 100mg drug
Wcfuof
aygestin 5mg brand - <a href="https://norethgep.com/bimatoprost/">where can i buy careprost</a> buy yasmin without a prescription
Rjznzv
fosamax 70mg usa - <a href="https://pilaxmax.com/">fosamax order</a> order provera 5mg generic
Rtweda
generic dostinex - <a href="https://adostilin.com/premarin/">premarin online</a> alesse brand
Rpbury
buy estrace 1mg generic - <a href="https://festrolp.com/anastrozole/">order anastrozole pill</a> anastrozole order
Hzusye
バイアグラ 薬局で買える - <a href="https://jpedpharm.com/">正規品バイアグラ錠の正しい処方</a> シアリス通販おすすめ
Dprvvy
プレドニン 薬局で買える - <a href="https://jpaonlinep.com/jamoxicillin/">アモキシシリン処方</a> アジスロマイシン 副作用
Xjkmqi
eriacta duke - <a href="https://eriagra.com/asildigrah/">sildigra pay</a> forzest blush