【问题标题】:Unable to convert BLOB to String无法将 BLOB 转换为字符串
【发布时间】:2015-03-20 12:59:01
【问题描述】:

在我的代码中,用户应该添加产品的详细信息,包括名称、图像、类别和其他详细信息。但是输入数据并保存后,它无法查看他们输入的数据。而是显示:无法将 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 "
        +")";

【问题讨论】:

  • 您的 Item 代码在哪里?发布它
  • 你的 table 代码是什么,意味着你为 Image 定义了哪个数据结构?
  • 我已经编辑了我的帖子

标签: java android sqlite


【解决方案1】:

使用以下代码

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

【讨论】:

  • 如果您使用 jdbc (Connector / J) 连接并设置参数 'useServerPrepStmts=true',getString("blob") 可能不适合您。 bugs.mysql.com/bug.php?id=84086
猜你喜欢
  • 2011-11-01
  • 2013-06-28
  • 2020-01-19
  • 2017-07-07
  • 1970-01-01
  • 2015-07-02
  • 2015-08-04
  • 1970-01-01
  • 2012-09-19
相关资源
最近更新 更多