Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if data exist in firebase. (KivyMD Python)

I'm new to Python and KivyMD. Also to working with Databases. I want to check if the data provided by the user using the KivyMD App is already in the Firebase Realtime Database. These are the data in the firebase.

Data in Firebase

The Code

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)

If the user enters an existing value in the database, that value should not be saved in the database. Also a Dialog box should be shown that the email is already in use. If the value provided by the user is not in the database it should be saved. Please help me to do this.

like image 347
Kusal Darshana Avatar asked Feb 11 '26 01:02

Kusal Darshana


2 Answers

Did you try with firebase_admin?

I check my data with like this:

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
    ...

If user email does not exist, query_email is going to be empty list. Do not forget that query_email is not json data. You can convert it json with to_dict():

email_query_result_as_json = query_email[0].to_dict()
like image 60
Barış Şenyerli Avatar answered Feb 15 '26 10:02

Barış Şenyerli


I'm guessing you use python-firebase which so far has very little documentation. So I'll try to do my best based on the package source...

You have to use firebase.get method to retrieve a dictionary with the current data stored for a given user, then check if that data contains the information you are going to add (untested, since firebase doesn't even import on my machine):

record = firebase.get('/Users', 'id')
if not record.get('Email'):
    firebase.post('/Users/'+'id', data)
like image 29
Nux ツ Avatar answered Feb 15 '26 11:02

Nux ツ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!