Firebase Database in SQL

How to implement BETWEEN Sql query in Firebase?

There's good documentation on how to do firebase queries as well as apply them to ranges.

Something like:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("table");

Query query = ref.orderByChild("price").startAt(min).endAt(max);

Then you add the appropriate listener for what you are trying to do. (e.g. a ValueEventListener or a ChildEventListener via addValueEventListener or addChildEventListener respectively.

How can I convert this Firebase database to SQL database?

You can have a 1-row and 1-column "objectIndex" table where the value can be updated by using an SQL Cron-Job.

You can then build a query with a cartesian product that returns your data as follows:

|----|------|-------|-------------|
| id | body | index | objectIndex |
|----|------|-------|-------------|
| 1 | foo1 | 1 | 25 |
|----|------|-------|-------------|
| 2 | foo2 | 1 | 25 |
|----|------|-------|-------------|
| 3 | foo3 | 2 | 25 |
|----|------|-------|-------------|

It is redundant but gets the job done. The code to retrieve these values can be written as follows:

SELECT id, body, index, objectIndex
FROM objectTable, objectIndexTable

How to make a query in Firebase similar to a SQL query?

In the world of NoSQL databases, a query that looks like this:

SELECT "name" FROM users WHERE email = "user@gmail.com"

Can be written in two ways. If you are using Firebase Realtime Database
, the query should look like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
Query query = usersRef.orderByChild("email").equalTo("user@gmail.com");

And if you are using Cloud Firestore, the query should look like this:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("users");
Query query = usersRef.whereEqualTo("email", "user@gmail.com");

Edit:

According to you comment, to get the name, please use the above lines along with the following lines of code:

ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
Log.d(TAG, name);
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
query.addListenerForSingleValueEvent(valueEventListener);

Can we use both SQL database and Firebase database while developing a web application?

Yes, you can use freely both databases for your application. One thing to remember is that when you are getting data from a Firebase database, you are getting it in an asynchronous way.

There is no restriction for using regarding Firebase and other databases. As long as you are using it correctly i recomand you using Firbease.

Hope it helps.

Can I use firebase for only auth and any other SQL database to store other user data?

Firebase Authentication isn't related in any way to Cloud Firestore. You can authenticate your users with Firebase and save the data in any database you want, even in an SQL database. However, Cloud Firestore has some benefits. So I recommend you check the official docs.



Related Topics



Leave a reply



Submit