【问题标题】:Sqlite saving image as blobSqlite 将图像保存为 blob
【发布时间】:2012-01-11 20:25:35
【问题描述】:

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

这里有错误信息。 12-02 20:38:26.291:I/数据库(22251): sqlite 返回:error code = 1, msg = no such column: 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; 
}

【问题讨论】:

    标签: android sqlite blob


    【解决方案1】:

    不确定这是否能解决问题,但请尝试在您的 create 语句中将 BLOB 更改为 blob(大写为小写)。

    【讨论】:

    • 哦..我通过更新数据库解决了。谢谢。但是,我再次卡住了保存图像。当我从相机中挑选图片并保存时,列表视图不仅一次制作了两项。第一个仍然没有显示图片,但第二个最终出现了选定的图片,但我根本无法修改图像..
    • 在更新数据库时您需要更改哪些内容?你说不能修改图片是指如何更新数据库中的记录?
    • 有时会保存图像,但有时不会。我真的很想知道。我可以通过电子邮件提供我的代码吗?你能帮我检查一下吗?我真的很抱歉。直到现在我都感到压力很大。如果可以的话,能给个邮箱吗?谢谢..
    猜你喜欢
    • 2011-08-30
    • 2015-11-15
    • 2022-01-24
    • 1970-01-01
    • 2017-04-15
    • 2017-06-10
    • 2015-06-19
    • 2014-06-27
    • 1970-01-01
    相关资源
    最近更新 更多