How to check for duplicate data in firebase

I am using firebase. I want to check for duplicate data for my application. I am currently allowing the user to enter a name and email address. But if the user enters the same name and email a second time, it is also added to the database. how to solve this problem Here is my code:

package com.example.zaid_pc.mpd2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class tournament extends AppCompatActivity {

private EditText etTournament;
private EditText emailTournament;
private DatabaseReference mDatabaseRefernce;
Button btnTournament;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tournament);

etTournament = (EditText)findViewById(R.id.etTournament);
emailTournament = (EditText)findViewById(R.id.emailTournament);
btnTournament = (Button)findViewById(R.id.btnTournament);

mDatabaseRefernce = FirebaseDatabase.getInstance().getReference().child("People Registered");

 btnTournament.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {



  final String name = etTournament.getText().toString().trim();
  final String email = emailTournament.getText().toString().trim();

  //mDatabaseRefernce.child("Name").setValue(name);
  //mDatabaseRefernce.child("Email").setValue(email);



final DatabaseReference peopleRegisered = mDatabaseRefernce.push();
peopleRegisered.child("Name").setValue(name);
peopleRegisered.child("Email").setValue(email);



Toast.makeText(tournament.this, "You will be notified via email", Toast.LENGTH_LONG).show();

finish();




        }
    });


}

      

}

+3


source to share


3 answers


Before transferring data to the database, first check with the database if the data exists or not

check username or not from following code

DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
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) {

    }
});

      



changed this code based on your needs

for link    Firebase Request Data

0


source


To solve this problem, I recommend that you use a unique ID for each user. This identifier can be a unique identifier generated by the method push()

, uid

or email address

. I offer you the last option. Since Firebase does not accept a character, for example .

in a key, you need to set up your email address like this:

name@email.com → name @email, com

To achieve this, let's look at two methods to help you encode

and decode

provide an email address.

private String encodeUserEmail(String userEmail) {
    return userEmail.replace(".", ",");
}

private String decodeUserEmail(String userEmail) {
    return userEmail.replace(",", ".");
}

      

Assuming the database structure looks like this:



root
 |
 --- users
        |
        --- name@email,com
                |
                --- uid: 4362454274745
                |
                --- name: John

      

you can check if the user exists using the following code:

DatabaseReference userEmailRef = FirebaseDatabase.getInstance().getReference().child("users").child(userEmail);
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                Log.d("TAG", "Users exists!");
            } else {
                //Create user
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    userEmailRef.addListenerForSingleValueEvent(valueEventListener);

      

In which userEmail

is the encoded email of the user.

Hope it helps.

0


source


I understand how below code you need to add one value sink here and check if the data you want to look for in the table exists or not.

mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(final DataSnapshot dataSnapshot) {
                for (DataSnapshot data : dataSnapshot.getChildren()) {
                 //If email exists then toast shows else store the data on new key
                    if (!data.getValue(User.class).getEmail().equals(email)) {
                        mFirebaseDatabase.child(mFirebaseDatabase.push().getKey()).setValue(new User(name, email));
                    } else {
                        Toast.makeText(ChatListActivity.this, "E-mail already exists.", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onCancelled(final DatabaseError databaseError) {
            }
        });

      

0


source







All Articles