无法将BLOB转换为String

时间:2015-03-20 12:59:01

标签: java android sqlite

在我的代码中,用户应该添加产品的详细信息,包括名称,图像,类别和其他详细信息。但是在输入数据并保存之后,它无法查看输入的数据,而是显示无法将BLOB转换为String。

我添加数据的编码。

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 "
        +")";

1 个答案:

答案 0 :(得分:5)

使用以下代码

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类型。