有使用过 Python tinydb 这个库的吗,求助

2018-05-28 16:37:06 +08:00
 chenqh

如何动态构建 query,
有时候要写个 demo server,所以用了 tinydb 这个库,但是有一个问题,如何动态构造 query 呢?

querys = []
for name in ("a", "b","c", "d"):
	val = self.get_argument(name,"")
    if val:
    	querys.append(where(name)==val)
list=db.search(querys)

有没有了办法实现上面那种效果呢?

2156 次点击
所在节点    Python
2 条回复
nbboy
2018-05-28 17:19:31 +08:00
太小众了把。
louisganlu
2018-05-30 16:09:39 +08:00
http://tinydb.readthedocs.io/en/latest/usage.html
The second, traditional way of constructing queries is as follows:

>>> from tinydb import where
>>> db.search(where('field') == 'value')
Using where('field') is a shorthand for the following code:

>>> db.search(Query()['field'] == 'value')
Accessing nested fields with this syntax can be achieved like this:

>>> db.search(where('birthday').year == 1900)
>>> db.search(where('birthday')['year'] == 1900)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/458406

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX