如何使用python遍历每个firebase数据库子级? [英] How to loop through each firebase database child with python?
本文介绍了如何使用python遍历每个firebase数据库子级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个Firebase数据结构
I have this firebase data structure
我想在firebase生成的密钥下打印所有密钥(即从3030-00-809-7702到newNSN),所以我使用此代码
I want to print all the keys under the firebase generated keys (i.e from 3030-00-809-7702 to newNSN) so I use this code
Inventory = db.child("Inventories").get()
for business in Inventory.each():
businessid = business.key()
productdb = db.child("Inventories").child(businessid).get()
for product in productdb.each():
productid = product.key()
print(businessid)
print(productid)
但是我得到的是这个
因此仅打印最后一个按键,而不是所有按键.我做错了什么,如何打印所有键?
so only the last keys are being printed instead of all the keys. What am I doing wrongly and how can I get it to print all the keys ?
推荐答案
只需在当前树上打印值即可获得整个内容
Just print the value at the current tree to get the whole thing
inventory = db.child("Inventories").get()
for business in inventory.each():
print(business.val())
或者您要进行迭代,这对于从N个孩子那里向Firebase请求N个项目的效率低下.
Or you go iterate it, which is really inefficient to request N items from Firebase for N children.
inventorydb = db.child("Inventories")
for businessid in inventorydb.shallow().get().each():
productdb = inventory.child(businessid)
# print the ids
print([id for id in productdb.shallow().get()])
这篇关于如何使用python遍历每个firebase数据库子级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文