从Firebase数据库中删除特定值

时间:2016-09-23 09:35:54

标签: android android-studio firebase firebase-realtime-database

我想删除Firebase数据库中的特定值以下是我已存储的数据库

enter image description here

现在,我想要删除以下值

fname: "John"

lname: "wick"

enter image description here

Here, What i have try to delete specific value from Firebase..

DatabaseReference demo = mReference.child("demo").orderByValue().equalTo("John").getRef();

    demo.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            dataSnapshot.getRef().removeValue();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

但这会删除数据库中的所有值...

Question如何删除该特定值???我必须做些什么

任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:2)

而不是使用addListenerForSingleValueEvent尝试使用addChildEventListener,如下所示,

Query queryRef = mReference.child("demo").orderByChild("fname").equalTo("John");

queryRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot snapshot, String previousChild) {
      snapshot.getRef().setValue(null);
    }
});

答案 1 :(得分:1)

您尝试使用此代码,

    mReference.child("demo").orderByChild("fname").equalTo("John").addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mReference.child("demo").child(dataSnapshot.getKey()).setValue(null);
        }

       @Override
         public void onCancelled(DatabaseError databaseError) {
              Log.w("TodoApp", "getUser:onCancelled", databaseError.toException());
         }
});

答案 2 :(得分:0)

你可以尝试一下:

BEGIN
    SET NOCOUNT ON;

     begin try 
        UPDATE projects
        SET 
            projectUser = @projectUser,
            projectStartDate = @projectStartDate
        where projectId = @projectId
        SELECT 'OK'
     end try

     begin catch
        SELECT 'ERROR - msg error'
     end catch    

    COMMIT

END

答案 3 :(得分:0)

非常感谢所有人,修改后有很多答案和查询..这对我有用。

mReference.child("demo").orderByChild("fname").equalTo("John").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            mReference.child("demo").child(dataSnapshot.getKey()).setValue(null);

            //also work
            //dataSnapshot.getRef().setValue(null);  
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

答案 4 :(得分:0)

尝试此代码。希望这对您有帮助 谢谢

database.getReference().child("Groups").child(groupItem.getGroupkey()).child("members").child(groupMember.getKey()).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
                                   @Override
                                   public void onSuccess(Void aVoid) {
                                       new CustomMessage(GroupDetailsActivity.this, "Successfully deleted");
                                   }
                               }).addOnFailureListener(new OnFailureListener() {
                                   @Override
                                   public void onFailure(@NonNull Exception e) {
                                       Customlog.showlogD("ERROR_MSG",e.getMessage());
                                   }
                               });

数据库结构:https://i.stack.imgur.com/Lw3Ia.png