Android sqlite delete query not working

The following SQL query does not remove identifiers starting from zero.

Android Sqlite Table Structure

String CREATE_TABLE_BUS = "CREATE TABLE " + TABLE_BUS + "(" + KEY_TID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_BUS_NUM + " TEXT," + KEY_BUS_NAME + " TEXT," + KEY_FROM + " TEXT," + KEY_TO + " TEXT," + KEY_TYPE + " TEXT" + ")"; db.execSQL(CREATE_TABLE_BUS); 

I saved BUS_NUM as text, and not as int for any purpose.

And this is the function that I call to delete the line.

  public void Delete_Bus(String bus_num) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_BUS, KEY_BUS_NUM+"="+bus_num , null); Log.e("deleting bus num", bus_num); db.close(); // Closing database connection } 

This code works very well when the code does not start from scratch.

It works for 76555, not 09877. What is wrong with my code.

+6
source share
3 answers

The request generated by this code ends as follows:

 DELETE FROM BusTable WHERE BusNum = 012345 

The database will interpret this as a number, not a string.

In SQL lines should be specified:

 DELETE FROM BusTable WHERE BusNum = '012345' 

However, you can avoid formatting the string with the parameter:

 db.delete(TABLE_BUS, KEY_BUS_NUM + " = ?", new String[] { bus_num }); 
+28
source

Can..

 public void Delete_Bus(String bus_num) { SQLiteDatabase db=this.getWritableDatabase(); db.execSQL("DELETE FROM "+TABLE_BUS+" WHERE "+KEY_BUS_NUM+"='"+bus_num+"'"); db.close(); } 
+9
source
  public void Remove_Bookmarked(Bookmark bookmark) { SQLiteDatabase db = this.getReadableDatabase(); db.delete(Bookmark_TABLE_NAME, COL_ID_CONTENT + "= ? and " + COL_TYPE + " = ?", new String[]{String.valueOf(bookmark.getId_content()), String.valueOf(bookmark.getType())}); } 
0
source

Source: https://habr.com/ru/post/1200420/


All Articles