I'm trying to fetch and use cursor.getString(1)
or cursor.getBlob(1)
in my query and I have error
Unable to convert BLOB to string
The problem is when my value is String
or let's say the value is null
and use cursor.getString(1)
it works fine since the value is like string and I use getString but what if the value is blob
and use cursor.getString(1)
it errors since it conflicts, on the other hand if I used cursor.getBlob(1)
sometimes I have a value which equals to null
it getting me an error since the value is string is not blob
how can I prevent this? need help
Query
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT evd.id,evd.additional_image,evd.overall_remarks FROM emv_validation_details AS evd WHERE evd.id=" id);
while (cursor.moveToNext()) {
myEdit.putString("emv_id_u",cursor.getString(0));
String additional_image = cursor.getString(1); //error here if the value is blob type
if(additional_image ==null){
additonal_image_u = null;
}
else{
additonal_image_u = cursor.getBlob(1);
}
myEdit.putString("overall_remarks_u",cursor.getString(2));
}
cursor.close();
CodePudding user response:
This could be an answer, if not it may give you a direction.
Try using an Object and get the return type example here
Don't know if it will work first up, you may need to tweak it but, I can't test it but you won't know if you don't try.
Object additional_image = "";
if((additional_image = cursor.getString(1)).getClass().toString() != null)
{
//TODO whatever
}
CodePudding user response:
Use the following code
java.sql.Blob ablob = rs.getBlob(1); String str = new String(ablob.getBytes(1l, (int) ablob.length()));
or
Try this (a2 is BLOB col)
PreparedStatement ps1 = conn.prepareStatement("update t1 set a2=? where id=1");
Blob blob = conn.createBlob();
blob.setBytes(1, str.getBytes());
ps1.setBlob(1, blob);
ps1.executeUpdate();