更新和删除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的位置。
我该如何做到这一点?我还没有尝试在Android中的任何代码。
要编写单个数据,您可以使用 setValue ()
方法在 DatabaseReference
上添加您的子标识:
private void writeNewData(String userId,String name,String email){
更新一个具体的值,你应该更简洁:
User user = new User(name,email);
mDatabase.child(users)。child(userId).setValue(user);
$ b $ p
$ b在你的情况下,你可以这样做:
<$ c
如果你想要的话,可以使用下面的代码:$ c $ mDatabase.child(UID2)。child(KEY2)。setValue(yourNewValueOrObject);
mDatabase.child(UID2)。child(KEY2)。child(email)。setValue(newEmail);
无论如何,我建议您使用自定义类作为POJO(Plain Old Java Object)与数据库中每个项目的值。例如:
public class User {
public String username;
public String email;
$ b public User(){
//调用DataSnapshot.getValue(User.class)所需的默认构造函数
}
public User用户名,字符串电子邮件){
this.username = username;
this.email = email;
$ b $ p $最后删除你应该使用的数据removeValue()
方法。
private void deleteUserData (String userId){
mDatabase.child(users)。child(userId).removeValue();
这个方法将从你的数据库中删除整个引用,所以要小心。在你想删除特定字段的情况下,你应该添加另一个
.child()
调用到树中。例如,假设我们要从KEY2节点删除电子邮件的值:
mDatabase.child(users)。child(userId).child(email)。最后,有可能我们想更新不同数据库节点中的多个字段。在这种情况下,我们应该使用带引用和值的映射的
updateChildren()
方法。
private void writeNewPost(String userId,String username,String title,String body){
//在/ user-posts / $ userid / $ postid和
/ / / posts / $ postid同时
String key = mDatabase.child(posts)。push()。getKey();
Post post = new Post(userId,username,title,body);
地图< 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< String,Object>
中的每一行调用setValue()
节点和Object的值。
您可以在官方 Firebase文档
This is my database:
{ "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" } }
I would like to update and delete data which for example would be at the position of "KEY2".
How can I achieve this? I have not yet tried any code in Android.
解决方案To write single data you can use the
setValue()
method on yourDatabaseReference
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); }
In your case you can do something like:
mDatabase.child("UID2").child("KEY2").setValue(yourNewValueOrObject);
If you want to update a specific value, you should be more concise:
mDatabase.child("UID2").child("KEY2").child("email").setValue(newEmail);
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; } }
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(); }
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();
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); }
What
updateChildren
method do. Is asetValue ()
call over each row in the givenMap<String, Object>
being the key the full reference of the node and the Object the value.You can read more update and delete data in the official Firebase documentation
这篇关于更新和删除Firebase中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!