Home > Net >  Check if string already exist in Firebase Realtime Database with Python
Check if string already exist in Firebase Realtime Database with Python

Time:07-18

I'm using Firebase in my Python program, and I'm successfully pushing user's details into database, but I want to make sure that those details are not repeating... Here's the code I'm using:

cred_obj = firebase_admin.credentials.Certificate(my_certificate)
        firebase_admin.initialize_app(cred_obj, {'databaseURL': "https://my-url.com"})
        ref = db.reference("/Users/Details")
        ref.push().set({'ID': randomNumber})

Everytime user has started this program, he's login in with his ID, so I don't want to push it to the database again and again...

How can I make my program to push ID only if it doesn't exist in database?

CodePudding user response:

Method 1

  cred_obj = firebase_admin.credentials.Certificate(my_certificate)
            firebase_admin.initialize_app(cred_obj, {'databaseURL': "https://my-url.com"})
            ref = db.reference("/Users/Details")
         check_user =  ref.order_by_child('ID').equal_to('your id').get())

// can use the if-else statement to identify

// checking if string is empty
print("Check if the user is exist : ", end="")

if(not check_user):
    print("The user not exists")
else:
    print("The user exists")

Method 2

Not Sure about this in python

 cred_obj = firebase_admin.credentials.Certificate(my_certificate)
                firebase_admin.initialize_app(cred_obj, {'databaseURL': "https://my-url.com"})
                ref = db.reference("/Users/Details")
             check_user =  ref.order_by_child('ID').equal_to('your id').exists())

.exists()

Maybe it will return a Bool

Check if value exists in firebase DB

  • Related