简体   繁体   中英

Check If a Particular Value Exists in the FireBase Database Using Python

I Create A RealTime DataBase And I Start Pushing some Data Like Username and Id and Date ext.

But I need to Check if This ID is Exist in my Database or not How Can I do it.

This is my Code:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db


cred_obj = firebase_admin.credentials.Certificate('firebase_credentials.json')


default_app = firebase_admin.initialize_app(cred_obj, {
    'databaseURL': 'My DataBase URL'})
ref = db.reference("/Logs")
ref.child(user.name).set({
            'Name' : user.full_name,
            'ID' : str(user.id),
            'Date' : str(update.message.date.astimezone(pytz.timezone('Asia/Damascus')).strftime("%d-%b-%Y")),
            'Time' : str(update.message.date.astimezone(pytz.timezone('Asia/Damascus')).strftime("%H:%M:%S"))
            })

这是我的实时数据库

I need to Check if this name or this ID Exist I won't Update the Database..

To conditionally write a value to a path in the database, you can use a transaction .

In your case that's look something like:

def set_initial_value(current_value):
    return current_value if current_value else {
        'Name' : user.full_name,
        'ID' : str(user.id),
        'Date' : str(update.message.date.astimezone(pytz.timezone('Asia/Damascus')).strftime("%d-%b-%Y")),
        'Time' : str(update.message.date.astimezone(pytz.timezone('Asia/Damascus')).strftime("%H:%M:%S"))
    }

ref = db.reference("/Logs")
try:
    ref.child(user.name).transaction(set_initial_value);
    print('Transaction completed')
except db.TransactionAbortedError:
    print('Transaction failed to commit')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM