【问题标题】:How to check if data exist in firebase. (KivyMD Python)如何检查firebase中是否存在数据。 (KivyMD Python)
【发布时间】:2021-11-06 06:35:49
【问题描述】:

我是 PythonKivyMD 的新手。也可以使用数据库。我想检查用户使用 KivyMD 应用程序提供的数据是否已经在 Firebase 实时数据库中。这些是 Firebase 中的数据。

守则

def send_data(self, email):
    from firebase import firebase

    firebase = firebase.FirebaseApplication("https://infinity-mode-default-rtdb.firebaseio.com/", None)

    data = {
        'Email' : email
    }

    if email.split() == []:
        cancel_btn_checkpoint_dialogue = MDFlatButton(text='Retry', on_release=self.close_checkpoint_dialogue)
        self.checkpoint_dialog = MDDialog(title='Access Denied', text="Invalid Username"),
                                          buttons=[cancel_btn_checkpoint_dialogue])

        self.checkpoint_dialog.open()

    else:
        firebase.post('Users', data)

如果用户在数据库中输入现有值,则不应将该值保存在数据库中。还应显示一个对话框,表明电子邮件已在使用中。如果用户提供的值不在数据库中,则应将其保存。请帮我做这件事。

【问题讨论】:

    标签: python firebase firebase-realtime-database kivy kivymd


    【解决方案1】:

    我猜你使用的是python-firebase,到目前为止它的文档很少。所以我会尽量根据包源做到最好……

    您必须使用 firebase.get 方法来检索包含为给定用户存储的当前数据的字典,然后检查该数据是否包含您要添加的信息(未经测试,因为 firebase 甚至不导入我的机器):

    record = firebase.get('/Users', 'id')
    if not record.get('Email'):
        firebase.post('/Users/'+'id', data)
    

    【讨论】:

    • @KusalDarshana 代码给出了什么错误?
    • @KusalDarshana 我不知道。您也可以尝试使用不同的模块,例如 pypi.org/project/firebase
    • 先生,用户给的值,就算有,也会一次又一次的保存。还有其他方法吗?
    • 先生,我什么都听不懂。你能解释一下吗?
    • @KusalDarshana 我不认为我可以我的答案。
    【解决方案2】:

    你试过firebase_admin吗?

    我这样检查我的数据:

    import firebase_admin
    from firebase_admin import credentials
    from firebase_admin import firestore
    
    cred = credentials.Certificate(
            "firestore-cred.json"
        )
    firebase_admin.initialize_app(cred)
    db = firestore.client()
    data = {
        "Email" : email
    }
    
    query_email = db.collection(u'Users') \
                    .where(u"Email",u"==",data["Email"]) \
                    .get()
    if query_email:  
        # exists
        ...
    else:
        # does not exist
        ...
    

    如果用户电子邮件不存在,query_email 将是空列表。 不要忘记query_email 不是 json 数据。您可以使用to_dict() 将其转换为json:

    email_query_result_as_json = query_email[0].to_dict()
    

    【讨论】:

    • 先生,db.collection(u'Users') 中的 db 是什么?是db = firebase.Database 还是from firebase_admin import db?还是别的什么?
    • @KusalDarshana 它是db = firestore.client()。我添加了 Firestore,但忘记了 db 的实例。一切都会好起来的。希望对您有所帮助。
    • 谢谢先生,它正在工作!但是,当用户输入现有电子邮件地址并输入另一个电子邮件时,会出现错误(ValueError: The default Firebase app already exists. This means you called initialize_app() more than once without providing an app name as the second argument. In most cases you only need to call initialize_app() once. But if you do want to initialize multiple apps, pass a second argument to initialize_app() to give each app a unique name.),因为它无效。我能做些什么来预防它?
    • @KusalDarshana 您只需要使用 1 个 firebase 库。选择其中之一。 firebase_admin 或您的旧框架 firebase。如果可行,请检查答案,并打开有关它的新问题并分享您的代码。这个答案不关心。
    • 非常感谢先生的好意帮助...
    【解决方案3】:

    尝试使用dataSnapshot.exist() 方法或snapshot.hasChild 方法,它们在python 中工作,我不能100% 确定它们是否在python 中工作,但值得一试。

    【讨论】:

    • “它们在 python 中工作,我不能 100% 确定它们是否在 python 中工作”——那么......它们是否在 Python 中工作?
    猜你喜欢
    • 2016-09-21
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    相关资源
    最近更新 更多