在我的代码中,用户应该添加产品的详细信息,包括名称、图片、类别和其他详细信息。但是输入数据并保存后,它无法查看他们输入的数据。相反,它显示:无法将 BLOB 转换为字符串。
我添加数据的编码。
items Item= new items();
db.getWritableDatabase();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte imageInByte[] = stream.toByteArray();
Bitmap bMap= BitmapFactory.decodeByteArray(imageInByte, 0, imageInByte.length);
ivThumbnailPhoto.setImageBitmap(bMap);
Item.setName(name.getText().toString());
Item.setCategory(SpCate.getSelectedItem().toString());
Item.setDetails(details.getText().toString());
Item.setImage(imageInByte);
db.addItemSpinner(new items());
// Save the Data in Database
MyDb entry= new MyDb(SellingItem.this);
entry.getWritableDatabase();
entry.addItemSpinner(Item);
entry.close();
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
Log.d("name", Item.getName());
Log.d("category", Item.getCategory());
Log.d("detailsL", Item.getDetails());
Intent i=new Intent(SellingItem.this,SearchItem.class);
startActivity(i);
我的数据库输入数据。
public void addItemSpinner(items i) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, i.name); // Contact Name
values.put(COLUMN_IMAGE, i.image); // Contact Phone
values.put(COLUMN_CATE, i.category); // Contact Name
values.put(COLUMN_DETAILS, i.details); // Contact Phone
// Inserting Row
db.insert(TABLE_NAME, null, values);
db.close(); // Closing database connection
}
logcat 显示了这一点。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectonline/com.example.projectonline.SearchItem}: android.database.sqlite.SQLiteException: unknown error (code 0): Unable to convert BLOB to string
这是我的元素代码。
package com.example.projectonline;
import android.graphics.Bitmap;
public class items {
int id;
String name, category, details;
byte[] image;
public items(){
}
public items(int id, String name, String category, byte[]image, String details)
{
this.id=id;
this.name= name;
this.category=category;
this.image= image;
this.details=details;
}
public items(String name, String category, byte[]image, String details){
this.name = name;
this.category = category;
this.image = image;
this.details = details;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Bitmap getBitmap() {
// TODO Auto-generated method stub
return null;
}
}
这就是我的数据库的样子
public static final String TAG = "MyDb";
public static final String TABLE_NAME="PRODUCT";
public static final String COLUMN_ID="id";
public static final String COLUMN_NAME="name";
public static final String COLUMN_CATE="category";
public static final String COLUMN_IMAGE="image";
public static final String COLUMN_DETAILS="details";
private static final String DATABASE_NAME="Spinner";
private static final int DATABASE_VERSION= 1;
private static final String SQL_CREATE_ITEM = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_NAME + " TEXT NOT NULL, "
+ COLUMN_CATE + " TEXT NOT NULL, "
+ COLUMN_IMAGE + " BLOB, "
+ COLUMN_DETAILS + " TEXT NOT NULL "
+")";
最佳答案
使用下面的代码
java.sql.Blob ablob = rs.getBlob(1);
String str = new String(ablob.getBytes(1l, (int) ablob.length()));
或
试试这个(a2 是 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();
即使没有 BLOB 也可以工作,驱动程序会自动转换类型:
ps1.setBytes(1, str.getBytes);
ps1.setString(1, str);
此外,如果您使用文本,CLOB 似乎是一种更自然的 col 类型。
关于java - 无法将 BLOB 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29167074/