如何使用android在firebase数据库中执行连接查询

时间:2017-04-10 09:53:41

标签: android firebase firebase-realtime-database

此处成员的组密钥是用户密钥。 任务:想要根据组的成员密钥从enter code here m用户获取comida_id。 这里我们尝试首先从Group中获取成员密钥。在获取成员密钥时,我们还希望在该成员的循环进行时获取用户和用户的comida_id。所以场景将是首先我们将基于Group的密钥从Group中获取所有密钥(例如createTime,groupName,members)。现在我们将对成员执行循环,这样就可以得到成员的密钥了。现在,当在该循环中获取此成员的键时,我们想要从User's获取comida_id,这将仅在该循环中获取。

GroupKey - >获取数据(createTime,groupName,成员,餐馆) - >获取成员密钥和comida_id(在循环中(获取成员密钥然后获取comida_id))

*我的代码

FirebaseDatabase mDatabase_gp = FirebaseDatabase.getInstance();
mDatabase_gp.getReference("Group/"+group_key).addValueEventListener(
                new ValueEventListener()
                {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot)
                    {

                        Map<String, Object> valuesMap = (HashMap<String, Object>) dataSnapshot.getValue();

                        Map userkey = (Map) valuesMap.get("members");
                        Log.d("Members", String.valueOf(userkey));

                        for (Object ke: userkey.keySet())
                        {
                            Log.d("runfirst","First Run");

                            String key = String.valueOf(ke.toString());
                            Log.d("Member key",key);


                            // HERE WHAT CORRESPONDS TO JOIN
                            FirebaseDatabase User = FirebaseDatabase.getInstance();
                            User.getReference("User/"+key).addValueEventListener(
                                    new ValueEventListener()
                                    {
                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot)
                                        {
                                            Map<String, Object> valuesMap = (HashMap<String, Object>) dataSnapshot.getValue();

                                            String userid = String.valueOf(valuesMap.get("comida_id"));
                                            Log.d("comida_id",userid);
                                            Toast.makeText(ChatSectionActivity.this,userid,Toast.LENGTH_SHORT).show();
                                        }
                                        @Override
                                        public void onCancelled(DatabaseError databaseError)
                                        {
                                        }
                                    }
                            );
                        }

                        Log.d("runlast","Last Run");
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError)
                    {

                    }
                }
        );

*

在上面的代码中,我们尝试实现相同的功能,但此处此代码将首先从组中获取所有成员键。获取所有成员后,它将在另一个循环中获取comida_ids。因此,将有两个不同的循环工作从成员获取comida_id。它不像连接查询那样工作。

GroupKey - &gt;获取数据(createTime,groupName,成员,餐馆) - &gt;获取成员密钥(在循环(获取成员密钥)之后获取comida_id。

* 1)我试过这个解决方案 - How to perform join query in Firebase?

**但是得到这个错误 -

Incompatible types.  
Required:   com.google.firebase.database.DatabaseReference 
Found:  com.google.firebase.database.ValueEventListener 

https://i.stack.imgur.com/ljlWo.png

1 个答案:

答案 0 :(得分:0)

Firebase没有加入查询。而你所做错的是你在Fireabaseuser上引用你的数据库,这是错误的。

mdatabaseReference.child("Answer").child("Answer"+QID).orderByChild("questionId").equalTo(QID).addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

            if (muftiAnswer_List.size() > 0)
                muftiAnswer_List.clear();

            for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {


                final AnswerClass muftiAnswer = postsnapshot.getValue(AnswerClass.class);


           String ide=muftiAnswer.getMuftiId(); //getting AlimId From Class object


                Log.e(TAG, "Alim:"+ muftiAnswer.getMuftiId());


                //Using Query to get Alim Name From Data Table.!
                mdatabaseReference.child("users").child("Alim").child(ide).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override      //.orderByChild("alimId").equalTo(user_Id)
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        Log.e(TAG, "AlimNode:");


                        for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
                        {

                            Log.e(TAG, "EqualNode:");


                            if(postSnapshot.getKey().equals("alimName"))
                            {
                                Log.e(TAG, "AlimNameNodeSnapShot:");

                                //Setting Alim Name in Current Object replcing id for name

                                muftiAnswer.setMuftiId(postSnapshot.getValue().toString());

                            }

                           // resultMuftiAnswer_List.add(muftiAnswer);

                        }//End of snapshot
                    }//ondata Change

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        // Failed to read value
                        Log.w(TAG, "Failed to read value.", databaseError.toException());
                    }
                });//End OF Alim Query
            }//end of comments

            circular_progress2.setVisibility(View.GONE);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(Details_Activity.this, "LOLOLOL", Toast.LENGTH_SHORT).show();
        }
    });

您必须使用嵌套循环来执行 MYSQL

等连接
Query query = reference.child("issue").orderByChild("id").equalTo(0);
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            // dataSnapshot is the "issue" node with all children with id 0
            for (DataSnapshot issue : dataSnapshot.getChildren()) {
                // do something with the individual "issues"
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

访问链接以便更好地理解

https://firebase.googleblog.com/2013/10/queries-part-1-common-sql-queries.html