简体   繁体   中英

android studio and sqlite check if username already exists

I don't know how to avoid repeated aadhar value. please help me anyone, it is very useful for my college project. i stored my data in sqlite database, i need, if we enter exists value means show alert msg like "this value already exists" and exit the page.

if anyone tell me how to modify my coding and give me some relevant coding.

Votingpage.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_votingpage);
        myDb = new DatabaseHelper(this);

        emailview = findViewById(R.id.emailview);
        edtaadhar1 = findViewById(R.id.edtaadhar1);
        btnlogout = findViewById(R.id.btnlogout);
        btnadd = findViewById(R.id.btnadd);
        radioGroup = findViewById(R.id.radioGroup);

        AddData();


        FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
        emailview.setText(firebaseUser.getEmail());

        btnlogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Votingpage.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }

        private void AddData() {
            btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (edtaadhar1.getText().toString().trim().length() <= 0) {
                    Toast.makeText(Votingpage.this, "Please enter aadhar no", Toast.LENGTH_SHORT).show();
                }else {


                    int radioid = radioGroup.getCheckedRadioButtonId();

                    radioButton = findViewById(radioid);

                    boolean isInserted = myDb.insertData(radioButton.getText().toString(),edtaadhar1.getText().toString());
                    if (isInserted = true)
                        Toast.makeText(Votingpage.this, "Thanks for giving your vote", Toast.LENGTH_SHORT).show();

                    else
                        Toast.makeText(Votingpage.this, "data not inserted", Toast.LENGTH_SHORT).show();


                }

            }
        });
    }

    public void check_button(View v){
        int radioid = radioGroup.getCheckedRadioButtonId();

        radioButton = findViewById(radioid);
    }
}  


DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Vote.db";
    public static final String TABLE_NAME = "voter_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "PARTY";
    public static final String COL_3 = "AADHAR";


    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,PARTY TEXT,AADHAR INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }


    public boolean insertData(String party,String aadhar) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, party);
        contentValues.put(COL_3, aadhar);

        long result = db.insert(TABLE_NAME, null, contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }

}

With this method you can add below your insertData() method, you can check the value is exist in your db.

private boolean isValueExist(String value){
    String query = "SELECT * FROM " + TABLE_NAME + " WHERE " + COL_3 + " = ?";
    String[] whereArgs = {value};

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(query, whereArgs);

    int count = cursor.getCount();

    cursor.close();

    return count >= 1;
}

If the count is 0 this method will return false so you can save your data.

To make it work you should modify your insertData() method like,

public boolean insertData(String party,String aadhar) {
    if(!isValueExist(addhar)){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, party);
        contentValues.put(COL_3, aadhar);

        long result = db.insert(TABLE_NAME, null, contentValues);
        return result != -1;
    } else {
        return false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM