How to remove blob element from table in sqlite android?

I send bytearray to insert the blob element, which it inserts as a png image, it works. But I want to remove the blob element for them, I send bytearray to the column, but not delete.

public void deleteImage(Byte[] bytearray) { try{ database.execSQL("DELETE FROM deleteImage "+" where image='"+bytearray+"';"); } catch(Exception e){ e.toString(); } } 
+2
source share
3 answers

you can try this

  database.execSQL("DELETE FROM deleteImage WHERE image=\""+bytearray+"\" ;") or mDb.delete(deleteImage,"image=\""+bytearray+"\"", null); mDb.delete(deleteImage,"image = '"+bytearray+"'", null); 
0
source

I had the same problem as you. I managed to find a solution for the request with blob in the where clause, but not for deletion. So at the end, my solution was the first to request the rows that I want to delete, and use the row id to delete it. Here is a sample code. I assume that you have a column named ImageId as the primary key of type int.

  public Cursor queryImage(final byte[] data, String[] columns) { return getReadableDatabase().queryWithFactory( new CursorFactory() { @Override public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) { query.bindBlob(1, data); return new SQLiteCursor(db, masterQuery, editTable, query); } }, false, "FavoriteData", columns, "Image = ?", null, null, null, null, null); } public void favoriteDelete(byte[] byteArray){ Cursor cursor = queryImage(byteArray, new String[]{"ImageId"}); while (cursor.moveToNext()) { getReadableDatabase().delete("FavoriteData", "ImageId" + "= ?", new String[] { String.valueOf(cursor.getInt(0))}); } } 

If anyone has a better solution, I will be interested too. In any case, if you have a way to avoid using Blob for the query, this would be better because it is not so efficient. You can try to identify the images in some way, for example, using the hash of the hash of the images to search.

0
source

Try this one (it works for me):

UUID is stored as BLOB

 db.execSQL("DELETE FROM "+DBHelper.USER_TABLE+" WHERE "+DBHelper.USER_UUID+"=?", new Object [] { uuidToByteArray(user.getUuid()) } 

Documentation: SQLiteDatabase execSQL .

According to the documentation, byte [] , String, Long and Double are supported in bindArgs. However, it is recommended that you do not use this method to execute SELECT / INSERT / UPDATE / DELETE statements. I do not know why.

 public void execSQL (String sql, Object[] bindArgs) 
0
source

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


All Articles