[英]Python - How do I get and use the data from firebase?
Here is my updated code with the revisions. 这是我带有修订的更新代码。 I only added the user_num = 1
to try to avoid the unsupported operand error I was getting , but that didn't work either. 我只添加了user_num = 1
来尝试避免遇到我不支持的操作数错误,但是那也不起作用。
.py 的.py
class MetropolisApp(App):
user_num = 1
database_url = 'https://metropolis-58211.firebaseio.com/'
def build(self):
self.my_firebase = MyFirebase()
return ControlScreens()
def on_start(self):
user_num_req = requests.get("https://metropolis-58211.firebaseio.com/user_num.json")
self.user_num = user_num_req.json()
db = requests.get('https://metropolis-58211.firebaseio.com/' + str(self.user_num) + '.json')
#print(db.ok)
data = json.loads(db.content.decode())
#print(data)
def post(self, firstname, lastname, email, username, password):
self.user_num += 1
# Make a python dictionary
fname_data = {"fname": firstname}
lname_data = {"lname": lastname}
email_data = {"email": email}
user_id = {"userid": username}
user_pw = {"userpw": password}
# Send the python dictionary
requests.post(url=self.database_url + str(self.user_num) + '.json', json=fname_data)
requests.post(url=self.database_url + str(self.user_num) + '.json', json=lname_data)
requests.post(url=self.database_url + str(self.user_num) + '.json', json=email_data)
requests.post(url=self.database_url + str(self.user_num) + '.json', json=user_id)
requests.post(url=self.database_url + str(self.user_num) + '.json', json=user_pw)
def in_database(self, firstname, lastname, email, username, password):
fname_data = {"fname": firstname}
lname_data = {"lname": lastname}
email_data = {"email": email}
user_id = {"userid": username}
user_pw = {"userpw": password}
requests.patch(url=self.database_url + str(self.user_num) + '.json', json=fname_data)
requests.patch(url=self.database_url + str(self.user_num) + '.json', json=lname_data)
requests.patch(url=self.database_url + str(self.user_num) + '.json', json=email_data)
requests.patch(url=self.database_url + str(self.user_num) + '.json', json=user_id)
requests.patch(url=self.database_url + str(self.user_num) + '.json', json=user_pw)
check_request = requests.get(
'https://metropolis-58211.firebaseio.com/.json?orderBy="userid"&equalTo="%s"' % self.root.ids[
'user_login'].text)
print(check_request.json())
# print(self.root.ids)
Not sure if the .kv file is needed, but here is the main code in which I am having errors. 不知道是否需要.kv文件,但这是我遇到错误的主要代码。
Edited Database 编辑数据库
There are a couple concepts to understand here. 这里有几个概念需要理解。 The first is the rule system of Firebase's real-time database. 首先是Firebase实时数据库的规则系统。 The rule system dictates what kind of reads/writes to the database are allowed. 规则系统指示允许对数据库进行哪种类型的读/写。 An important part of the rule system is using indexes
to tell Firebase that you want to be able to query your data by a certain parameter. 规则系统的重要部分是使用indexes
来告诉Firebase您希望能够通过特定参数查询数据。 For example, you need to query your database by the userid
parameter. 例如,您需要通过userid
参数查询数据库。 In your rules tab of your real-time database, you need to tell Firebase to let you 'indexOn' the 'userid' parameter, like so: 在实时数据库的“规则”标签中,您需要告诉Firebase让您“ indexOn”“ userid”参数,如下所示:
{
"rules": {
".read": true, // Note - this allows all reads from anyone anywhere. only use for testing
".write": true, // same comment
".indexOn": ["userid"] // You can allow multiple indexOns by doing ["userid","foo","bar"]
}
}
The ".indexOn" portion above is part of your top level rules ( /
in your database), and you want to index on data that is inside some special identifier ( /special_identifier/userid
). 上面的“ .indexOn”部分是您的顶级规则( /
在数据库中)的一部分,并且您希望对某些特殊标识符( /special_identifier/userid
)内的数据建立索引。 The special identifier for your test user is 2
in this case. 在这种情况下,测试用户的特殊标识符为2
。
Now that you are allowing your database to be queried by the userid
parameter, you need to actually send a request using python to query it. 现在,您可以通过userid
参数查询数据库,您实际上需要使用python发送请求来查询它。 The next concept here is sending some special arguments in the URL of your get request (read Firebase's documentation here ). 这里的下一个概念是在您的get请求的URL中发送一些特殊的参数(请在此处阅读Firebase的文档)。 In this case, you need to try to get data from your top level ( /
) location, and orderBy userid
and only return values equalTo self.root.ids['user_login'].text
在这种情况下,您需要尝试从顶级( /
)位置获取数据,并使用orderBy userid
并仅返回equalTo self.root.ids['user_login'].text
值self.root.ids['user_login'].text
Your URL in this case would be: 在这种情况下,您的网址为:
'https://metropolis-58211.firebaseio.com/.json?orderBy="userid"&equalTo="%s"'%self.root.ids['user_login'].text
Notice how you use the ?
注意您如何使用?
character to specify you're going to send some special parameters, and the &
means another special parameter is coming. 字符来指定您要发送一些特殊参数,而&
表示即将出现另一个特殊参数。 Also make sure your values that you are ordering-by or equaling-to are surrounded in "
(assuming their strings in your database, which they are at the moment). 另外,请确保您要排序或等于的值用"
括起来(假定它们在数据库中的字符串,此刻它们是当前字符串)。
When you do a get request to that URL, either it will return the data for that user, or an empty dictionary. 当您对该URL进行获取请求时,它将返回该用户的数据或一个空字典。
check_request = requests.get('https://metropolis-58211.firebaseio.com/.json?orderBy="userid"&equalTo="%s"'%self.root.ids['user_login'].text)
print(check_request.json()) # If this has data, the user exists, if it's an empty dictionary, {}, the user doesn't exist
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.