How do I search (SELECT and WHERE) in the firebase database?

3

I have a database like this:

{
  "message" : "Hello, World!",
  "reserva" : [ null, {
    "codreserva" : 123,
    "email" : "[email protected]",
    "status" : "check in"
  }, {
    "codreserva" : 124,
    "email" : "[email protected]",
    "status" : "check out"
  } ]
}

And I have a code to look up the reservation status:

public void ProcuraReserva(String codigodareserva){
// Read from the database
myRef = database.getReference("reserva/"+codigodareserva+"/status");
myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String value = dataSnapshot.getValue(String.class);
        textview1.setText("Status da reserva "+ value);
    }

    @Override
    public void onCancelled(DatabaseError error) {
        textview1.setText("Status com falha");
    }
  });
}

However, it looks for in ID of my reservation wanted it to search according to CODRESERVA . Could someone help me in this question to search according to my "line" in this database of Firebase .

    
asked by anonymous 15.09.2016 / 14:21

2 answers

2

Try using child ()

Firebase child

myRef.child("codreserva").child(codigoreserva).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    String value = dataSnapshot.getValue(String.class);
    textview1.setText("Status da reserva "+ value);
}

@Override
public void onCancelled(DatabaseError error) {
    textview1.setText("Status com falha");
}
    
15.09.2016 / 15:02
0

What Fabio Nobre said, that's right. N Forget that you have to pass the email parameter that is within .equalTo. To receive values, in your case, I recommend that you do:

  String value = dataSnapshot.child("nome").getValue().toString();
    
31.05.2017 / 00:58