In this Python Firebase lesson we are going to learn about Python Firebase Real Time Database, The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. When you build cross-platform apps or web applications , all of your clients share one Realtime Database instance and automatically receive updates with the newest data.
Note: before this make sure that you have enabled firebase real time database in your firebase console.
In here we are going to create firebase real time database, and after that we are going to save the data, to save data with a unique, auto-generated, timestamp-based key, use the push()
method.
1 2 3 4 5 |
db = firebase.database() data = {"name":"Parwiz Forogh"} db.child("users").push(data) print("Data added to real time database ") |
To create your own keys use the set()
method. The key in the example below is “OwnKey”.
1 |
db.child("users").child("OwnKey").set(data) |
To update data for an existing entry use the update()
method.
1 2 3 4 5 |
db.child("users").child("OwnKey").update({"name":"John Doe"}) print("Data updated successfully ") db.child("users").child("-LzqIcMVMPaQKVLLjK5d").update({"name":"Updated Name"}) print("Data updated successfully ") |
Queries return a PyreResponse object. Calling val()
on these objects returns the query data. Calling key()
returns the key for the query data.
1 2 |
users = db.child("users").get() print(users.val()) |
Each Returns a list of objects on each of which you can call val()
and key()
.
1 2 3 4 5 |
all_users = db.child("users").get() for users in all_users.each(): print(users.val()) print(users.key()) |
For deleting data for an existing entry use the remove()
method.
1 2 |
db.child("users").child("-LzqIcMVMPaQKVLLjK5d").remove() print("User removed") |
This is the complete code for Python Firebase Real Time Database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import pyrebase config = { "apiKey": "AIzaSyCDf21HwvKFVRmSApuXFdZAHCYcGmPcEF8", "authDomain": "test-f302c.firebaseapp.com", "databaseURL": "https://test-f302c.firebaseio.com", "projectId": "test-f302c", "storageBucket": "test-f302c.appspot.com", "messagingSenderId": "85719903313", "appId": "1:85719903313:web:23723ce4e3b3503f201c70", "measurementId": "G-XBHPSEFYRK" } firebase = pyrebase.initialize_app(config) db = firebase.database() data = {"name":"Parwiz Forogh"} #db.child("users").push(data) #print("Data added to real time database ") #add your ownkey #db.child("users").child("OwnKey").set(data) #update the data in real time database db.child("users").child("OwnKey").update({"name":"John Doe"}) print("Data updated successfully ") db.child("users").child("-LzqIcMVMPaQKVLLjK5d").update({"name":"Updated Name"}) print("Data updated successfully ") #get all the data from the database users = db.child("users").get() print(users.val()) all_users = db.child("users").get() for users in all_users.each(): print(users.val()) print(users.key()) #removing the data from real time database #db.child("users").child("-LzqIcMVMPaQKVLLjK5d").remove() #print("User removed") |