更新和删除 Firebase 中的数据 [英] Update and delete data in Firebase
问题描述
这是我的数据库:
{
"UID1" : {
"KEY" : {
"Name" : "name1",
"Email" : "something1@something.com",
"userid" : "UID1"
}
},
"UID2" : {
"KEY2" : {
"Name" : "name1",
"Email" : "something1@something.com",
"userid" : "UID2"
},
"KEY3" : {
"Name" : "name2",
"Email" : "something2@something.com",
"userid" : "UID2"
},
"KEY4" : {
"Name" : "name3",
"Email" : "something3@something.com",
"userid" : "UID2"
}
}
我想更新和删除数据,例如KEY2"位置的数据.
I would like to update and delete data which for example would be at the position of "KEY2".
我怎样才能做到这一点?我还没有在 Android 中尝试过任何代码.
How can I achieve this? I have not yet tried any code in Android.
推荐答案
要写入单个数据,您可以使用 DatabaseReference
上的 setValue()
方法和您孩子的 ID:
To write single data you can use the setValue()
method on your DatabaseReference
with your child Id's:
private void writeNewData(String userId, String name, String email) {
User user = new User(name, email);
mDatabase.child("users").child(userId).setValue(user);
}
在您的情况下,您可以执行以下操作:mDatabase.child("UID2").child("KEY2").setValue(yourNewValueOrObject);
In your case you can do something like:
mDatabase.child("UID2").child("KEY2").setValue(yourNewValueOrObject);
如果你想更新一个特定的值,你应该更简洁:mDatabase.child("UID2").child("KEY2").child("email").setValue(newEmail);
If you want to update a specific value, you should be more concise:
mDatabase.child("UID2").child("KEY2").child("email").setValue(newEmail);
无论如何,我建议您使用自定义类作为 POJO(Plain Old Java Object)以及数据库中每个项目的值.例如:
Anyway I recomend you to use custom classes as POJO's(Plain Old Java Object) with the values of each of your items in database. For example:
public class User {
public String username;
public String email;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String username, String email) {
this.username = username;
this.email = email;
}
}
最后要删除数据,您应该以相同的方式使用 removeValue()
方法.
Finally to remove data you should use the removeValue()
method in the same way.
private void deleteUserData(String userId) {
mDatabase.child("users").child(userId).removeValue();
}
此方法将从您的数据库中删除整个引用,因此请小心使用.如果您想删除特定字段,您应该向树添加另一个 .child()
调用.例如,假设我们要从KEY2"节点中删除电子邮件值:mDatabase.child("users").child(userId).child("email").removeValue();
This method will remove the whole reference from your Database, so be care with it. In the case that you wanted to remove a specific field, you should add another .child()
call to the tree. For example, let's say that we want to remove the email value from "KEY2" node:
mDatabase.child("users").child(userId).child("email").removeValue();
最后,可能我们想要更新不同数据库节点中的多个字段.在这种情况下,我们应该使用带有引用和值映射的 updateChildren()
方法.
Finally, there's the case that maybe we want to update multiple fields in different database nodes. In that case we should use the updateChildren()
method with a map of references and values.
private void writeNewPost(String userId, String username, String title, String body) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String key = mDatabase.child("posts").push().getKey();
Post post = new Post(userId, username, title, body);
Map<String, Object> postValues = post.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
mDatabase.updateChildren(childUpdates);
}
updateChildren
方法做什么.是对给定 Map
中每一行的 setValue ()
调用,作为节点的完整引用的键和对象的值.
What updateChildren
method do. Is a setValue ()
call over each row in the given Map<String, Object>
being the key the full reference of the node and the Object the value.
您可以在官方 Firebase 文档中阅读更多更新和删除数据
这篇关于更新和删除 Firebase 中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!