更新 Firebase 数据库数据

huangapple go评论118阅读模式
英文:

Update Firebase database data

问题

我正在尝试更新我的数据库:

更新 Firebase 数据库数据

这是我的代码,但似乎无法更新数据库(在Firebase设置中设置了写入访问权限为true)

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("items").child("1").child("itemAvailability").setValue("Not Available");

Java类

public class Item {
    String itemName;
    String itemAvailability;

    public Item(String itemName, String itemAvailability) {
        this.itemName = itemName;
        this.itemAvailability = itemAvailability;
    }

    public Item() {
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemAvailability() {
        return itemAvailability;
    }

    public void setItemAvailability(String itemAvailability) {
        this.itemAvailability = itemAvailability;
    }
}
英文:

I am trying to update my databse:

更新 Firebase 数据库数据

This is my code, but it doesn't seem to update the database. (Write access is set to true in Firebase settings)

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
      ref.child("items").child("1").child("itemAvailability").setValue("Not Available");

Java class

public class Item {
    String itemName;
    String itemAvailability;

    public Item(String itemName, String itemAvailability) {
        this.itemName = itemName;
        this.itemAvailability = itemAvailability;
    }

    public Item() {
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemAvailability() {
        return itemAvailability;
    }

    public void setItemAvailability(String itemAvailability) {
        this.itemAvailability = itemAvailability;
    }
}

答案1

得分: 2

你可以使用 Map 数据结构来实现类似的操作。

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

DatabaseReference itemRef = ref.child("items/1/itemAvailability");
Map<String, Object> itemUpdates = new HashMap<>();
itemUpdates.put("itemAvailability", "Not Available");

itemRef.updateChildrenAsync(itemUpdates);

这将获取物品 1 的可用性并将其更新为“不可用”。

英文:

You can do something like this with a Map Data structure.

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

DatabaseReference itemRef = ref.child(&quot;items/1/itemAvailability&quot;);
Map&lt;String, Object&gt; itemUpdates = new HashMap&lt;&gt;();
itemUpdates.put(&quot;itemAvailability&quot;, &quot;Not Available&quot;);

itemRef.updateChildrenAsync(itemUpdates);

This will take the item 1's availability and update it's availability to Not Available

答案2

得分: 0

以下是Firebase实时数据库插入、更新和删除操作(CRUD)的代码示例:

//****更新
var database = firebase.database();
let userRef = database.ref('myroot/items/');
userRef.child("1").child("itemAvailability").update("Not Available").then().catch();

//****插入
var database = firebase.database();
let userRef = database.ref('myroot/items/');
userRef.child("1").child("itemAvailability").set("Not Available").then().catch();

//****删除
let itemId = 1;
let userRef = database.ref('myroot/items/' + itemId);
userRef.remove().then().catch();
英文:

Here is the code for Firebase real-time database Insert, Update & Delete operation(CRUD).

//****UPDATE 
var database = firebase.database();
let userRef = database.ref(&#39;myroot/items/&#39;);
userRef.child(&quot;1&quot;).child(&quot;itemAvailability&quot;).update(&quot;Not Available&quot;).then().catch();

//****Insert 
var database = firebase.database();
let userRef = database.ref(&#39;myroot/items/&#39;);
userRef.child(&quot;1&quot;).child(&quot;itemAvailability&quot;).set(&quot;Not Available&quot;).then().catch();

//****Delete 
let itemId=1;
let userRef = database.ref(&#39;myroot/items/&#39; + itemId);
userRef.remove().then().catch();

答案3

得分: 0

Sure, here's the translated code:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("items").child("1").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        HashMap<String, Object> updateMap = new HashMap<>();
        updateMap.put("itemAvailability", "不可用");
        ref.updateChildren(updateMap);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {

    }
});

Please note that I've only translated the code portion as you requested. If you have any more code you'd like translated or any questions, feel free to ask.

英文:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
      ref.child(&quot;items&quot;).child(&quot;1&quot;).addListenerForSingleValueEvent(new ValueEventListener() {
         @Override
           public void onDataChange(@NonNull DataSnapshot snapshot) {
              HashMap&lt;String, Object&gt; updateMap = new HashMap&lt;&gt;();
              updateMap.put(&quot;itemAvailability&quot;, &quot;Not Available&quot;);
              ref.updateChildren(updateMap);
              }

         @Override
              public void onCancelled(@NonNull DatabaseError error) {

             }
        });

Might work for you.

huangapple
  • 本文由 发表于 2020年8月28日 06:26:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63624941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定