Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite deleting a specific row from database

I am trying to do a simple login page where I am able to delete a specific contact with its details just by entering the username.

In other words, I would like to delete a specific row in my database.

    public int deleteUsername(String content){

    String whereClause = KEY_CONTENT1 + "= '" + content + "'";

    return sqLiteDatabase.delete(MYDATABASE_TABLE, whereClause, null);
}

public int deletePassword(String content){

    String whereClause = KEY_CONTENT2 + "= '" + content + "'";

    return sqLiteDatabase.delete(MYDATABASE_TABLE, whereClause, null);
}

  public void onClick(View v) {

    String username = null;
    String password = null;

    username = this.txtnewuserinputedituser.getText().toString().toUpperCase();
    password = this.txtnewpasswordinputedituser.getText().toString().toUpperCase();


    switch (v.getId())
    {

    case R.id.btndeleteuser :
    {   


        mySQL.openToWrite();
        mySQL.deleteUsername(username);
        mySQL.deletePassword(password);;
        mySQL.close();

        String toastText = "User Deleted";
        Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT).show();

        break;
    }
    }
    }

Meaning that I would have to enter the username and password in the editview in order to delete that specific user. I found a few sources telling me to use something like this but I tried and it doesn't work. Is there any error?

public int deleteRow(String showId) {
    String table_name = "MYDATABASE_TABLE";
    String where = "showId='"+showId+"'";
    return sqLiteDatabase.delete(table_name, where, null);
}

I would like to delete a specific row in my database just by taking the username. I input as the id for the row to be deleted. How to do so, or is there any alternatives that I can use?

like image 843
alwin chng Avatar asked Dec 02 '22 20:12

alwin chng


2 Answers

You don't need to delete every entry in the row separately. The following method should delete the row where the username field equals the passed userName. The password field and any other field in the row will be removed also.

public void deleteUser(String userName)
{
    SQLiteDatabase db = this.getWritableDatabase();
    try
    {
        db.delete(MYDATABASE_TABLE, "username = ?", new String[] { userName });
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        db.close();
    }
}

EDIT: if you just copy the method, you won't have to open and close the DB manually any more, so your lines

mySQL.openToWrite();
mySQL.deleteUsername(username);
mySQL.deletePassword(password);;
mySQL.close();

would become just

mySQL.deleteUser(username);
like image 92
Thrakbad Avatar answered Dec 04 '22 08:12

Thrakbad


you can use simple SQL query to delete specific row Acc to sql

DELETE FROM table_name WHERE some_column=some_value; so use

db.execSQL("delete from " + Message.TABLENAME +" where specific_row_to_delete=\'" + user_name );

and if you have more then one where condition then you can use AND like

db.execSQL("delete from  " + TABLENAME +
            " where specific_row_to_delete=\'" + user_name +
            "\' and condition_to_delete  >= " + condtion_argument );
like image 45
John smith Avatar answered Dec 04 '22 08:12

John smith