Krishna Kumar Sharma
Krishna Kumar Sharma

Reputation: 35

How to create a custom database in android-fire-base with the custom format to store data

I want to create a database in fire-base like this:

database{
uid1{
name
age
}
uid2{
......}
} 

and so on. since I'm new to fire-base. i have only achieved this through some of the docs and examples.

database{
uid
}

where database is the root of the database.

How do I obtain the given structure?

Any help would be appreciated ?

Upvotes: 1

Views: 860

Answers (2)

Rob Winters
Rob Winters

Reputation: 161

You get the structure for your database by writing the values. No schema required, which is awesome. It should look something like this:

var rootRef = new Firebase('https://yourdb.firebaseio.com');

var user = rootRef.getAuth();
var userRef = rootRef.child('my-users').child(user.uid);
var myUsers = userRef.child('personal-info').push();
   myUsers.update({
     name: "Tom",
     age: "28"
   });

This will get you:

{
  "my-users" : {
    "067f75bf-4a07-473e-82e5-d9a5ee11be17" : {
      "personal-info" : {
        "-KN2dG5X4lLpp0fwfsXK" : {
          "name" : "Tom",
          "age" : "28"
        }
      }
    }
}    

Note: using push gives you a randomly generated key which can be helpful (but is not necessary)

Upvotes: 2

Sagar
Sagar

Reputation: 72

The Firebase structure is implemented through your code. You will simply have to create a reference to your remote database by:

private DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();

You will then have to use 'push()' method to create new child. You can follow up on the following two links to fit your requirements:

  1. https://firebase.google.com/docs/database/android/save-data

  2. https://github.com/firebase/quickstart-android/tree/master/database/app/src/main/java/com/google/firebase/quickstart/database

Hope that helps!

Upvotes: 1

Related Questions