sqlite的节能形象的blob [英] Sqlite saving image as blob

查看:176
本文介绍了sqlite的节能形象的blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想图像保存为BLOB类型。
我不知道这是正确的。当我调试,fetchAllItems()和fetchItem()不工作。
有没有搞错?我找不到它在所有..
对不起,长的代码,因为我无法找到斑点问题。
请帮帮我。谢谢..

下面的错误消息。
12-02 20:38:26.291:I /数据库(22251):
SQLite的返回:错误code = 1,味精=没有这样的列:图片

 公共类FridgeDatabaseHelper扩展SQLiteOpenHelper
{
    私有静态最后弦乐DATABASE_NAME =fridge_db;
    私有静态最终诠释DATABASE_VERSION = 1;    //创建数据库,通过SQL语句    私有静态最后弦乐DATBASE_CREATE =CREATE TABLE fridge_table+
            (_id整数主键自动增量,+
            一类的文字不为空,名称文本不为空,++
            EXPIRED_DATE文本不为空,图像BLOB);;    公共FridgeDatabaseHelper(上下文ctxt)
    {
        //我们可以把数据库文件名的DATABASE_NAME
        超(ctxt,DATABASE_NAME,空,DATABASE_VERSION);
    }
    // onCreate方法
    @覆盖
    公共无效的onCreate(SQLiteDatabase DB)
    {
        //方法是创建数据库中调用
        db.execSQL(DATBASE_CREATE);    }//数据库字段
公共静态最后弦乐KEY_ROWID =_id;
公共静态最后弦乐KEY_CATEGORY =类别;
公共静态最后弦乐KEY_NAME =名;
公共静态最后弦乐KEY_EXPIRED_DATE =EXPIRED_DATE;
公共静态最后弦乐KEY_IMAGE =形象;
私有静态最后弦乐DATABASE_TABLE =fridge_table;私人语境ctxt;
私人SQLiteDatabase分贝;
私人FridgeDatabaseHelper dbhelper;// SimpleDateFormat的日期格式=新的SimpleDateFormat(DD-MM-YYYY HH:MM:SS);公共FridgeDbAdapter(上下文ctxt)
{
    this.ctxt = ctxt;
}//打开数据库
公共FridgeDbAdapter的open()抛出的SQLException
{
    dbhelper =新FridgeDatabaseHelper(ctxt);
    DB = dbhelper.getWritableDatabase();
    返回此;
}//关闭数据库
公共无效的close(){
    dbhelper.close();
}//创建一个新项目
众长insertItem(字符串类,字符串名称,字符串EXPIRED_DATE,字节[]图片)
{
    ContentValues​​ initialValues​​ =新ContentValues​​();
    initialValues​​.put(KEY_CATEGORY,类别);
    initialValues​​.put(KEY_NAME,名);
    initialValues​​.put(KEY_EXPIRED_DATE,EXPIRED_DATE);
    initialValues​​.put(KEY_IMAGE,图像);
    返回db.insert(DATABASE_TABLE,空,initialValues​​);
}//返回光标放在数据库中的所有项目名单
公共光标fetchAllItems(){
    返回db.query(DATABASE_TABLE,新的String []
                   {KEY_ROWID,KEY_CATEGORY,KEY_NAME,KEY_EXPIRED_DATE,KEY_IMAGE},
                   NULL,NULL,NULL,NULL,NULL);
}//返回位于定义项的光标
公共光标fetchItem(长ROWID)抛出的SQLException
{
    光标mCursor = db.query(真,DATABASE_TABLE,
    新的String [] {KEY_ROWID,KEY_CATEGORY,KEY_NAME,KEY_EXPIRED_DATE,KEY_IMAGE},
    KEY_ROWID +=+ ROWID,NULL,NULL,NULL,NULL,NULL);
    //字节[]图像= NULL;
    如果(mCursor!= NULL){
        mCursor.moveToFirst();
        //图像= mCursor.getBlob(mCursor.getColumnIndexOrThrow(KEY_IMAGE));
    }
    返回mCursor;
}


解决方案

不能完全确定这是否会解决这个问题,但尝试在create语句改变BLOB到BLOB(大写小写)。

I am trying to save image as blob type. I am not sure it is right. When I debugging, fetchAllItems() and fetchItem() is not working. Is there any mistaken? I cannot find it at all.. Sorry for long coding because I cannot find problem with blob. Please help me. Thanks..

Error message here. 12-02 20:38:26.291: I/Database(22251): sqlite returned: 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; 
}

解决方案

Not exactly sure if this will solve it but try changing BLOB to blob (uppercase to lowercase) in your create statement.

这篇关于sqlite的节能形象的blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆