Android SQLite removes a specific row from a database

I am trying to make a simple login page where I can delete a specific contact with its data by simply entering a 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; } } } 

The value is that I will need to enter a username and password in editview in order to delete this particular user. I found several sources that told me to use something like this but I tried and it does not work. Is there a mistake?

 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 enter as the identifier of the row to be deleted. How to do this, or are there any alternatives I can use?

+4
source share
3 answers

You do not need to delete each entry in a row separately. The next method is to remove the line in which the username field is equal to the username passed. The password field and any other field in the line will also be deleted.

 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 no longer have to open and close the database, so your rows

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

will become simple

 mySQL.deleteUser(username); 
+15
source

you can use a simple SQL query to delete a specific string 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 than one where clause, you can use AND as

 db.execSQL("delete from " + TABLENAME + " where specific_row_to_delete=\'" + user_name + "\' and condition_to_delete >= " + condtion_argument ); 
+3
source

Try this if the where clause contains different types of values.

 public void delete(int Cid,String username,String password) { SQLiteDatabase database = this.getWritableDatabase(); database.delete(TABLE_NAME, KEY_CID + "=" + Cid + " and " + KEY_UserName + "=" + username + " and " + KEY_Password + "=" + password, null); database.close(); } 

OR

  public void delete(int Cid,String username,String password) { SQLiteDatabase database = this.getWritableDatabase(); database.execSQL("DELETE FROM " + TABLE_NAME+ " WHERE " +KEY_CID+ "= '"+Cid+"'AND "+KEY_Password+"='"+password+"'" + "AND "+KEY_UserName+"='"+username+"'"); database.close(); } 

pass parameters in the delete() method, from where you call this method.

Example

 favourite.delete(Courseid,Username,Password); 

Here, favourite is the database object that we created in our activity() , from which we call delete() function

0
source

All Articles