Learning Firebase(2):CRUD Our Database with Python
3 min readJan 6, 2020
Learning Firebase series:
Learning Firebase(1):Create Your First Project
In the last article we have known the method to set up the first project.
In this article, we will connect to our database with Python and start CRUD data(Create, Read, Update, Delete)
Firstly, we should install the package “python-firebase” via pip
pip install python-firebase
Test if the package can be imported
from firebase import firebase
Oops!!! SyntaxError
from .async import process_pool
^
SyntaxError: invalid syntax
Here is how to deal with it
Okay, how to update our data to the database
create a python file (ex: firebase_test.py)
from firebase import firebase
import timenew_users = [
{'name': 'Richard Ho'},
{'name': 'Tom Wu'},
{'name': 'Judy Chen'},
{'name': 'Lisa Chang'}
]db_url = 'https://XXXXXXX.firebaseio.com'
fdb = firebase.FirebaseApplication(db_url, None)
for user in new_users:
fdb.post('/user', user)
time.sleep(3)
change the db_url to your project URL
you can find it on Setting-> Service Account
After execute the python program, you could find that the database has been updated.
(Create)database(fdb.post)
#.post method give random key and valuefrom firebase import firebase
db_url = 'https://XXXXXX.firebaseio.com'
fdb = firebase.FirebaseApplication(db_url, None)
.
.
.We skip the code in this part
.
.
.
fdb.post('/invlotto/' + inv_month, inv_lotto)
(Read)database from Python(fdb.get)
from firebase import firebase
db_url = 'https://XXXXXX.firebaseio.com'
fdb = firebase.FirebaseApplication(db_url, None)
.
.
.We skip the code in this part
.
.
.
exist_data = fdb.get('/invlotto/'+inv_month, None)
(Update)database from python(fdb.patch or fdb.put)
#.put method can assign specific key and valuefrom firebase import firebase
db_url = 'https://XXXXXX.firebaseio.com'
fdb = firebase.FirebaseApplication(db_url, None)fdb.put('/users', '1')
(Delete)database from Python(fdb.delete)
from firebase import firebase
db_url = 'https://XXXXXX.firebaseio.com'
fdb = firebase.FirebaseApplication(db_url, None)fdb.delete('/users', '1')
I don’t want everyone can update my database, what should I do?
Go back to the rule page of this project
//Only users who have log in can access to this database// Change your rule from:
{
"rules": {
".read": true,
".write": true
}
}// to below:
{
"rules": {
".read": true,
".write": "auth !== null"
}
}
In the Authentication panel, click the button below
and select the method you want
Authentication
#Don't have the Authentication to READ or Write
from firebase import firebase
firebase = firebase.FirebaseApplication('https://your_storage.firebaseio.com', authentication=None)result = firebase.get('/users', None, {'print': 'pretty'})
print(result)
{'error': 'Permission denied.'}#Have the Authentication to READ or Writeauthentication = firebase.FirebaseAuthentication('THIS_IS_MY_SECRET', 'ozgurvt@gmail.com', extra={'id': 123})
firebase.authentication = authentication
print(authentication.extra)
{'admin': False, 'debug': False, 'email': 'ozgurvt@gmail.com', 'id': 123, 'provider': 'password'}user = authentication.get_user()
print(user.firebase_auth_token)
"eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJhZG1pbiI6IGZhbHNlLCAiZGVidWciOiBmYWxzZSwgIml
hdCI6IDEzNjE5NTAxNzQsICJkIjogeyJkZWJ1ZyI6IGZhbHNlLCAiYWRtaW4iOiBmYWxzZSwgInByb3ZpZGVyIjog
InBhc3N3b3JkIiwgImlkIjogNSwgImVtYWlsIjogIm96Z3VydnRAZ21haWwuY29tIn0sICJ2IjogMH0.lq4IRVfvE
GQklslOlS4uIBLSSJj88YNrloWXvisRgfQ"result = firebase.get('/users', None, {'print': 'pretty'})
print(result)
{'1': 'John Doe', '2': 'Jane Doe'}