Search code examples
javaandroidsqliteandroid-sqlitedelete-row

How to delete a user from sqlite database in android studio?


Im trying to figure out how I can delete one user at a time by typing in the edittext the users username and clicking deleting which I then want all the users information (username, usernum, password, birthdate, phone, address) to be deleted from the database. Below is my code and for some reason it isnt working can any one please please help me!! Im very desperate and ive been trying to figure out the problem for hours.

DatabaseHelperUser class:

public class DatabaseHelperUser extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "User.db";
public static final String TABLE_NAME = "User_table";
public static final String COL1 = "ID";
public static final String COL2 = "UserNum";
public static final String COL3 = "UserName";
public static final String COL4 = "Password";
public static final String COL5 = "BirthDate";
public static final String COL6 = "Phone";
public static final String COL7 = "Address";


public DatabaseHelperUser(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, UserNum TEXT,UserName Text,Password Text,BirthDate Text,Phone Text,Address Text)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public Cursor getData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
    return res;
}

public boolean deleteData(String UserName) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_NAME, "UserName" + "=?" + UserName, null) > 0;

}


}

RemoveUser class:

public class RemoveUser extends AppCompatActivity {

Button btdelete;
EditText txtUser;

DatabaseHelperUser myDb;

private String selectedName;


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

    btdelete = (Button) findViewById(R.id.butRemove);
    txtUser = (EditText) findViewById(R.id.etxtUserName);


    myDb = new DatabaseHelperUser(this);

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

            boolean delete = myDb.deleteData(txtUser.getText().toString());

            if(delete == true)
            Toast.makeText(RemoveUser.this,"User has been deleted", Toast.LENGTH_LONG).show();

            else
                Toast.makeText(RemoveUser.this,"User has not been deleted", Toast.LENGTH_LONG).show();

        }
    });


}
}

Solution

  • You must pass the variable UserName as the 3d argument of the method delete(), so it will replace the placeholder ? when the statement is executed:

    public boolean deleteData(String UserName) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABLE_NAME, "UserName = ?", new String[] {UserName}) > 0;
    }