分享
三行代码  ›  专栏  ›  技术社区  ›  WhatZitToyya

Firebase Android查询结构

  •  -3
  • WhatZitToyya  · 技术社区  · 4 天前

    我是firebase新手,如何转换查询:

    "Select A from Temp where B = Arr[0]"
    

    请帮帮我

    1 回复  |  直到 4 天前
        1
  •  0
  •   Alibek Jakupov    4 天前

    下面的示例代码将帮助您更好地理解该过程:

    // get login and password
    login = loginField.getText().toString().toLowerCase();
    password = passwordField.getText().toString();
    // create a database reference to find a user
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usersdRef = rootRef.child("users");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String statusMessage = "";
            // get all the instances from the database
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.child("name").getValue(String.class).toLowerCase();
                String basepassword = ds.child("password").getValue(String.class);
                if (name.equals(login) && basepassword.equals(password)) {
                    // if user is found then create a new instance
                    String surname = ds.child("surname").getValue(String.class);;
                    String status = ds.child("status").getValue(String.class);;
                    String email = ds.child("email").getValue(String.class);;
                    String sex = ds.child("sex").getValue(String.class);;
                    String birthday = ds.child("birthday").getValue(String.class);
                    break;
                } else {
                    // if the user is not found then return null
                    statusMessage="User not found";
                }
            }
            Toast.makeText(WelcomeActivity.this, statusMessage, Toast.LENGTH_LONG).show();
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    usersdRef.addListenerForSingleValueEvent(eventListener);