简体   繁体   中英

how to include a variable in tinydb query in python?

from tinydb import TinyDB, Query
db = TinyDB('db.json')
ddd = Query()
kl = free
output = db.search(ddd.state == 'free')

How to use variable k1 in tinydb db.search?

Just use it in db.search

from tinydb import TinyDB, Query
db = TinyDB('db.json')
ddd = Query()
db.insert({'state': 'free', 'field': 1})
db.insert({'state': 'n', 'field': 2})
db.insert({'state': 'free', 'field': 3})
kl = 'free'
output = db.search(ddd.state == kl)
print(output)

output

[{'state': 'free', 'field': 1}, {'state': 'free', 'field': 3}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM