【问题标题】:How to join two tables in firebase?如何在firebase中加入两个表?
【发布时间】:2019-11-26 18:36:29
【问题描述】:

我创建了一个具有studId, studName, place 的表。我还想创建另一个与具有studId, day, timeone, timeTwo 的第一个表相关的表。我该怎么做?我是 firebase 的初学者

这个方法会为firebase添加数据

private void addDate(){

    String studName = userName.getText().toString().trim();
    String place = spinnerPlace.getSelectedItem().toString();

    if(!TextUtils.isEmpty(studName)){

     String id = databaseStudentData.push().getKey();

     StudentData studentData = new StudentData(id, studName, place);

     databaseStudentData.child(id).setValue(studentData);
     addDay(id);

     Toast.makeText(this, "Data is saved", Toast.LENGTH_LONG).show();

    }else {
        Toast.makeText(this, "you Should Enter your name", Toast.LENGTH_LONG).show();
    }
}

这里是我存储的数据 enter image description here

【问题讨论】:

标签: java android firebase firebase-realtime-database


【解决方案1】:

只为一个属性创建另一个表没有任何好处,您可以执行以下操作。既然你已经这样做了:

     String id = databaseStudentData.push().getKey();

     StudentData studentData = new StudentData(id, studName, place);

     databaseStudentData.child(id).setValue(studentData);

现在要在此表下添加time,您可以执行以下操作:

 Map<String, Object> childUpdates = new HashMap<>();
 childUpdates.put("time", time);
 databaseStudentData.child(id).updateChildren(childUpdates);

这样您将拥有以下数据库:

id
 studId : "id"
 studName : "name"
 studPlace : "place"
 studTime : "time"

【讨论】:

  • childUpdates.put("day", day); nosql 中没有 join,如果您希望一个节点下的数据位于另一个节点下,那么您需要再次执行setValue,例如:DatabaseReference ref = FirebaseDatabase.getInstance().getReference("timing");ref.child(id).child("studId").setValue(id);ref.child(id).child("time").setValue(time);ref.child(id).child("studName").setValue(name); 等...例如。 ..
  • @SaraAlmaqbali 正如我在评论中所说,如果您希望一个节点下的数据也位于另一个节点下 + 附加数据,那么您需要创建两个引用并为每个引用使用 setValue() . nosql中没有join,可以多处添加数据。您应该阅读以下内容:firebase.googleblog.com/2013/04/…
猜你喜欢
  • 1970-01-01
  • 2018-12-17
  • 2015-04-28
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 2021-06-25
  • 2023-03-10
  • 2020-03-05
相关资源
最近更新 更多