Removing a row in SQLite on Android

This might be a dumb question, but I'm new to SQLite and I can't figure it out. I have 1 table with columns KEY_ROWID , KEY_NAME , KAY_LATITUDE and KEY_LONGITUDE . I want the user to be able to select one and delete it; Can someone give me directions to start? My question is the actual deletion of a line with only its name.

Relevant Code:

 public class BeaconDatabase { public static final String KEY_ROWID = "_id"; public static final String KEY_NAME = "beacon_name"; public static final String KEY_LATITUDE = "beacon_lat"; public static final String KEY_LONGITUDE = "beacon_lon"; private static final String DATABASE_NAME ="BeaconDatabase"; private static final String DATABASE_TABLE ="beaconTable"; private static final int DATABASE_VERSION = 1; private DbHelper helper; private final Context context; private SQLiteDatabase db; public BeaconDatabase(Context context) { this.context = context; } public BeaconDatabase open() { helper = new DbHelper(this.context); db = helper.getWritableDatabase(); return this; } public void close() { helper.close(); } public long createEntry(String name, Double lat, Double lon) { ContentValues cv = new ContentValues(); cv.put(KEY_NAME, name); cv.put(KEY_LATITUDE, lat); cv.put(KEY_LONGITUDE, lon); return db.insert(DATABASE_TABLE, null, cv); } public void deleteEntry(long row) { // Deletes a row given its rowId, but I want to be able to pass // in the name of the KEY_NAME and have it delete that row. //db.delete(DATABASE_TABLE, KEY_ROWID + "=" + row, null); } public String getData() { String[] columns = { KEY_ROWID, KEY_NAME, KEY_LATITUDE, KEY_LONGITUDE }; Cursor cursor = db.query(DATABASE_TABLE, columns, null, null, null, null, null); String result = ""; int iRow = cursor.getColumnIndex(KEY_ROWID); int iName = cursor.getColumnIndex(KEY_NAME); int iLat = cursor.getColumnIndex(KEY_LATITUDE); int iLon = cursor.getColumnIndex(KEY_LONGITUDE); for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { result += cursor.getString(iRow) + ": " + cursor.getString(iName) + " - " + cursor.getDouble(iLat) + " latitude " + cursor.getDouble(iLon) + " longitude\n"; } return result; } private static class DbHelper extends SQLiteOpenHelper { public DbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT NOT NULL, " + KEY_LATITUDE + " DOUBLE, " + KEY_LONGITUDE + " DOUBLE);" ); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); onCreate(db); } } } 
+87
android sqlite delete-row
Sep 22 2018-11-11T00:
source share
17 answers

You can try like this:

  //---deletes a particular title--- public boolean deleteTitle(String name) { return db.delete(DATABASE_TABLE, KEY_NAME + "=" + name, null) > 0; } 

or

 public boolean deleteTitle(String name) { return db.delete(DATABASE_TABLE, KEY_NAME + "=?", new String[]{name}) > 0; } 
+160
Sep 22 '11 at 6:21
source share

Try to get your decision

 String table = "beaconTable"; String whereClause = "_id=?"; String[] whereArgs = new String[] { String.valueOf(row) }; db.delete(table, whereClause, whereArgs); 
+150
Sep 22 '11 at 6:26
source share

other values ​​are better;

 db.delete("tablename","id=? and name=?",new String[]{"1","jack"}); 

this is similar to using this command:

 delete from tablename where id='1' and name ='jack' 

and using the delete function in this way is good because it removes the SQL injection.

+53
May 04 '15 at 6:58
source share

As long as I understand your question, you want to set two conditions for choosing the line to be deleted. To do this, you need to do:

 public void deleteEntry(long row,String key_name) { db.delete(DATABASE_TABLE, KEY_ROWID + "=" + row + " and " + KEY_NAME + "=" + key_name, null); /*if you just have key_name to select a row,you can ignore passing rowid(here-row) and use: db.delete(DATABASE_TABLE, KEY_NAME + "=" + key_name, null); */ } 
+17
Sep 22 '11 at 6:16
source share

Try this code

 public void deleteRow(String value) { SQLiteDatabase db = this.getWritableDatabase(); db.execSQL("DELETE FROM " + TABLE_NAME+ " WHERE "+COlUMN_NAME+"='"+value+"'"); db.close(); } 
+12
Apr 6 '16 at 9:46 on
source share

Try this code ...

 private static final String mname = "'USERNAME'"; public void deleteContact() { db.delete(TABLE_CONTACTS, KEY_NAME + "=" + mname, null); } 
+8
Feb 07 '13 at 7:27
source share

This works great:

 public boolean deleteSingleRow(String rowId) { return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; } 

You can also refer to this example .

+5
Apr 27 '12 at 9:37
source share

Guys, this is a general method that you can use for all your tables, works great in my case.

 public void deleteRowFromTable(String tableName, String columnName, String keyValue) { String whereClause = columnName + "=?"; String[] whereArgs = new String[]{String.valueOf(keyValue)}; yourDatabase.delete(tableName, whereClause, whereArgs); } 
+2
Jul 29 '15 at 10:15
source share

To remove rows from a table, you must provide selection criteria that identify the rows for the delete() method. The mechanism works in the same way as the selection arguments for the query() method. It divides the selection specification into a selection clause (where clause) and selection arguments.

  SQLiteDatabase db = this.getWritableDatabase(); // Define 'where' part of query. String selection = Contract.COLUMN_COMPANY_ID + " =? and " + Contract.CLOUMN_TYPE +" =? "; // Specify arguments in placeholder order. String[] selectionArgs = { cid,mode }; // Issue SQL statement. int deletedRows = db.delete(Contract.TABLE_NAME, selection, selectionArgs); return deletedRows;// no.of rows deleted. 

The return value for the delete() method indicates the number of rows that were deleted from the database.

+2
Mar 29 '18 at 13:26
source share

Guys, if the above solutions do not work for you, try this also because it worked for me.

 public boolean deleteRow(String name) { return db.delete(DATABASE_TABLE, KEY_NAME + "='" + name +"' ;", null) > 0; } 
+1
Jul 21 '16 at 12:23
source share

if you are using SQLiteDatabase then there is a delete method

Delete definition

 int delete (String table, String whereClause, String[] whereArgs) 

Implementation example

Now we can write the delete method with argument as name

 public void delete(String value) { db.delete(DATABASE_TABLE, KEY_NAME + "=?", new String[]{String.valueOf(value)}); } 

if you want to delete all records and then just pass null to the above method,

 public void delete() { db.delete(DATABASE_TABLE, null, null); } 

The source of information

+1
Dec 16 '16 at
source share

Works great!

 public void deleteNewMelk(String melkCode) { getWritableDatabase().delete(your_table, your_column +"=?", new String[]{melkCode}); } 
+1
Feb 17 '18 at 6:57
source share

Try the following:

 public void deleteEntry(long rowId) { database.delete(DATABASE_TABLE , KEY_ROWID + " = " + rowId, null);} 
0
Jun 01 '15 at 20:37
source share
 public boolean deleteRow(long l) { String where = "ID" + "=" + l; return db.delete(TABLE_COUNTRY, where, null) != 0; } 
0
Nov 30 '16 at 3:53
source share

You can do something like this by sharing your piece of working code

Make sure the query is similar to this

REMOVE FROM tableName WHERE KEY__NAME = 'parameter ToMatch'

 public void removeSingleFeedback(InputFeedback itemToDelete) { //Open the database SQLiteDatabase database = this.getWritableDatabase(); //Execute sql query to remove from database //NOTE: When removing by String in SQL, value must be enclosed with '' database.execSQL("DELETE FROM " + TABLE_FEEDBACKS + " WHERE " + KEY_CUSTMER_NAME + "= '" + itemToDelete.getStrCustName() + "'" + " AND " + KEY_DESIGNATION + "= '" + itemToDelete.getStrCustDesignation() + "'" + " AND " + KEY_EMAIL + "= '" + itemToDelete.getStrCustEmail() + "'" + " AND " + KEY_CONTACT_NO + "= '" + itemToDelete.getStrCustContactNo() + "'" + " AND " + KEY_MOBILE_NO + "= '" + itemToDelete.getStrCustMobile() + "'" + " AND " + KEY_CLUSTER_NAME + "= '" + itemToDelete.getStrClusterName() + "'" + " AND " + KEY_PRODUCT_NAME + "= '" + itemToDelete.getStrProductName() + "'" + " AND " + KEY_INSTALL_VERSION + "= '" + itemToDelete.getStrInstalledVersion() + "'" + " AND " + KEY_REQUIREMENTS + "= '" + itemToDelete.getStrRequirements() + "'" + " AND " + KEY_CHALLENGES + "= '" + itemToDelete.getStrChallenges() + "'" + " AND " + KEY_EXPANSION + "= '" + itemToDelete.getStrFutureExpansion() + "'" + " AND " + KEY_COMMENTS + "= '" + itemToDelete.getStrComments() + "'" ); //Close the database database.close(); } 
0
Feb 14 '17 at 11:10
source share

Try below code-

 mSQLiteDatabase = getWritableDatabase();//To delete , database should be writable. int rowDeleted = mSQLiteDatabase.delete(TABLE_NAME,id + " =?", new String[] {String.valueOf(id)}); mSQLiteDatabase.close();//This is very important once database operation is done. if(rowDeleted != 0){ //delete success. } else { //delete failed. } 
0
Jan 24 '18 at 13:09
source share

I have a problem deleting my data in a ListView. Here is the code to save the data, but cannot delete:

 final SQLiteDatabase db = mHelper.getWritableDatabase(); final ContentValues values = new ContentValues(); values.put(InputContract.TaskEntry.COL_TASK_TITLE, input); db.insertWithOnConflict(InputContract.TaskEntry.TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE); db.close(); updateUI(); 

Who knows what the problem is?

How can I delete some data?

0
Dec 08 '18 at 22:55
source share



All Articles