java 无法将 BLOB 转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29167074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:47:04  来源:igfitidea点击:

Unable to convert BLOB to String

javaandroidsqlite

提问by aki

In my code, user supposed to add details of a product including name, image, category, and other detail. But after entering the data and saved it, it cannot view the data that they input in, instead it is showed unable to convert BLOB to String.

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

My coding for adding data.

我用于添加数据的编码。

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

My Database to input data.

我的数据库输入数据。

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 shows like this.

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

This is my items code.

这是我的物品代码。

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;
    }

}

And this is my DB looks like

这是我的数据库看起来像

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

回答by

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)

试试这个(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();

It may work even without BLOB, driver will transform types automatically:

即使没有 BLOB 它也可以工作,驱动程序会自动转换类型:

 ps1.setBytes(1, str.getBytes);
 ps1.setString(1, str);

Besides if you work with text CLOB seems to be a more natural col type.

此外,如果您使用文本 CLOB 似乎是一种更自然的 col 类型。