简体   繁体   English

使用python-firebase查询Firebase

[英]Query Firebase using python-firebase

How does one query Firebase using Python? 如何使用Python查询Firebase? I am using python-firebase and can do simple gets (eg all entities) but need to query for a subset of entities. 我正在使用python-firebase并且可以执行简单的获取(例如所有实体),但需要查询实体的子集。 Any pointers would be helpful. 任何指针都会有所帮助。 I have successfully done this using JavaScript BTW. 我已经使用JavaScript BTW成功完成了这项工作。

You can now use Firebase Admin Python API to get at the database 您现在可以使用Firebase Admin Python API来访问数据库

https://firebase.googleblog.com/2017/07/accessing-database-from-python-admin-sdk.html?m=1 https://firebase.googleblog.com/2017/07/accessing-database-from-python-admin-sdk.html?m=1

from firebase_admin import db

root = db.reference()
x = db.reference('users/{0}'.format(new_user.key)).get()
print 'Name:', x['name']
print 'Since:', x['since']

python-firebase isn't keeping up to date with firebase api change python-firebase没有跟上firebase api的变化

Latest commit over 2 years ago 2年前的最新提交

To request an specific node from firebase you need to specify it in your URL, in the following example I will load a specific node (-KeuhkMH8SQcYJHatRnD) from a main 'news' node: 要从firebase请求特定节点,您需要在URL中指定它,在以下示例中,我将从主“新闻”节点加载特定节点(-KeuhkMH8SQcYJHatRnD):

def load_news():

    try:
        loader = urllib.request.urlopen("https://<YOUR-PROJECT-ID>.firebaseio.com/news/-KeuhkMH8SQcYJHatRnD.json")
    except urllib.error.URLError as e:
        message = json.loads(e.read())
        print(message["error"])
    else:
        print(loader.read())  

use Pyrebase, it's simple and easy to understand (in my opinion) 使用Pyrebase,它简单易懂(在我看来)

data = db.child("sensor").order_by_child("temp").limit_to_last(10).get()
print(data.val())

result : 结果:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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