Sqlite将图像保存为blob

时间:2011-12-02 20:59:16

标签: android sqlite blob

我正在尝试将图像保存为blob类型。 我不确定这是对的。当我调试时,fetchAllItems()和fetchItem()不起作用。 有什么不对吗?我根本找不到它.. 很抱歉长编码,因为我找不到blob的问题。 请帮我。感谢..

此处出现错误消息。 12-02 20:38:26.291:I / Database(22251): sqlite返回:错误代码= 1,msg =没有这样的列:image

public class FridgeDatabaseHelper extends SQLiteOpenHelper
{
    private static final String DATABASE_NAME = "fridge_db";
    private static final int DATABASE_VERSION = 1;

    //Database creates through sql statement

    private static final String DATBASE_CREATE = "create table fridge_table " +
            "(_id integer primary key autoincrement, " + 
            "category text not null, name text not null, "+"" +
            "expired_date text not null, image BLOB);";

    public FridgeDatabaseHelper(Context ctxt)
    {
        //we can put database file name on 'DATABASE_NAME'
        super(ctxt, DATABASE_NAME, null, DATABASE_VERSION);
    }


    //onCreate method
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        //Method is called during creation of the database
        db.execSQL(DATBASE_CREATE);

    }

//Database fields
public static final String KEY_ROWID = "_id";
public static final String KEY_CATEGORY = "category";
public static final String KEY_NAME = "name";
public static final String KEY_EXPIRED_DATE = "expired_date";
public static final String KEY_IMAGE = "image";
private static final String DATABASE_TABLE = "fridge_table";

private Context ctxt;
private SQLiteDatabase db;
private FridgeDatabaseHelper dbhelper;

//SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss");

public FridgeDbAdapter(Context ctxt)
{
    this.ctxt = ctxt;
}

//Open database
public FridgeDbAdapter open() throws SQLException
{
    dbhelper = new FridgeDatabaseHelper(ctxt);
    db = dbhelper.getWritableDatabase();
    return this;
}

//Close database
public void close(){
    dbhelper.close();
}

//Create a new item
public long insertItem(String category, String name, String expired_date, byte[] image)
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_CATEGORY, category);
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_EXPIRED_DATE, expired_date);
    initialValues.put(KEY_IMAGE, image);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

//return cursor over the list of all items in the database
public Cursor fetchAllItems(){
    return db.query(DATABASE_TABLE, new String[]
                   {KEY_ROWID, KEY_CATEGORY, KEY_NAME, KEY_EXPIRED_DATE, KEY_IMAGE}, 
                   null, null, null, null, null);
}

//return a cursor positioned at the defined item
public Cursor fetchItem(long rowId) throws SQLException
{
    Cursor mCursor = db.query(true, DATABASE_TABLE, 
    new String[]{KEY_ROWID, KEY_CATEGORY, KEY_NAME, KEY_EXPIRED_DATE, KEY_IMAGE}, 
    KEY_ROWID + "=" + rowId, null, null, null, null, null);
    //byte[] image = null;
    if(mCursor != null){
        mCursor.moveToFirst();
        //image = mCursor.getBlob(mCursor.getColumnIndexOrThrow(KEY_IMAGE));
    }
    return mCursor; 
}

1 个答案:

答案 0 :(得分:0)

不确定这是否会解决它,但尝试在创建语句中将BLOB更改为blob(大写为小写)。