android– 如何在DBAdapter中创建一个类型为BLOB的列的表

android– 如何在DBAdapter中创建一个类型为BLOB的列的表,第1张

概述我有一个全局DBAdapter,也有每个表一个.在我的全局DB-Adapter中,我在列中添加了“BLOB”类型.但是我没有得到我在DBAdapter中为特定表格所做的改变.我需要改变它,它也适用于BLOB类型.所以你走了:全局DBAdapter:packagede.retowaelchli.filterit.database;importandroid.conte

我有一个全局DBAdapter,也有每个表一个.在我的全局DB-Adapter中,我在列中添加了“BLOB”类型.但是我没有得到我在DBAdapter中为特定表格所做的改变.我需要改变它,它也适用于BLOB类型.

所以你走了:

全局DBAdapter:

package de.retowaelchli.filterit.database;import androID.content.Context;import androID.database.sqlException;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;import de.retowaelchli.filterit.database.ADFilterDBAdapter;public class DBAdapter {    public static final String DATABASE_name = "filterit";    public static final int DATABASE_VERSION = 1;    public static final String CREATE_table_ADFILTER = "create table adfilter (_ID integer primary key autoincrement, "    + ADFilterDBAdapter.name+","    + ADFilterDBAdapter.KEYWORD+","    + ADFilterDBAdapter.CACHE + ");";    private static final String CREATE_table_SFILTER = "create table sfilter (_ID integer primary key autoincrement, "    +SFilterDBAdapter.name+","    +SFilterDBAdapter.KEYWORD+","    +SFilterDBAdapter.SMILEY+ ");";    private static final String CREATE_table_ADMESSAGES = "create table admessages (_ID integer primary key autoincrement, "    +MessagesDBAdapter.PHONENUMBER+","    +MessagesDBAdapter.MESSAGE+ ");";    //HERE I CHANGED IT TO BLOB!    private static final String CREATE_table_SMILEY = " create table smiley (_ID integer primary key autoincrement, "    +SmileyDBAdapter.soURCE+" BLOB ,"    +SmileyDBAdapter.INFO+ ");";    private final Context context;     private DatabaseHelper DBHelper;    private sqliteDatabase db;    /**     * Constructor     * @param ctx     */    public DBAdapter(Context ctx)    {        this.context = ctx;    }    private static class DatabaseHelper extends sqliteOpenHelper     {        DatabaseHelper(Context context)         {            super(context, DATABASE_name, null, DATABASE_VERSION);        }        @OverrIDe        public voID onCreate(sqliteDatabase db)         {            db.execsql(CREATE_table_ADFILTER);            db.execsql(CREATE_table_SFILTER);            db.execsql(CREATE_table_ADMESSAGES);            db.execsql(CREATE_table_SMILEY);        }        @OverrIDe        public voID onUpgrade(sqliteDatabase db, int oldVersion,         int newVersion)         {                           // Adding any table mods to this guy here        }    }    /**     * open the db     * @return this     * @throws sqlException     * return type: DBAdapter     */    public DBAdapter open() throws sqlException     {        this.DBHelper = new DatabaseHelper(this.context);        this.db = this.DBHelper.getWritableDatabase();        return this;    }    /**     * close the db      * return type: voID     */    public voID close()     {        this.DBHelper.close();    }}

这是我表的DBAdapter:

package de.retowaelchli.filterit.database;import androID.content.ContentValues;import androID.content.Context;import androID.database.Cursor;import androID.database.sqlException;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;public class SmileyDBAdapter {        public static final String ROW_ID = "_ID";        public static final String SOURCE = "source";        public static final String INFO = "info";        private static final String DATABASE_table = "admessages";        private DatabaseHelper mDbHelper;        private sqliteDatabase mDb;        private final Context mCtx;        private static class DatabaseHelper extends sqliteOpenHelper {            DatabaseHelper(Context context) {                super(context, DBAdapter.DATABASE_name, null, DBAdapter.DATABASE_VERSION);            }            @OverrIDe            public voID onCreate(sqliteDatabase db) {            }            @OverrIDe            public voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {            }        }        /**         * Constructor - takes the context to allow the database to be         * opened/created         *          * @param ctx         *            the Context within which to work         */        public SmileyDBAdapter(Context ctx) {            this.mCtx = ctx;        }        public SmileyDBAdapter open() throws sqlException {            this.mDbHelper = new DatabaseHelper(this.mCtx);            this.mDb = this.mDbHelper.getWritableDatabase();            return this;        }        /**         * close return type: voID         */        public voID close() {            this.mDbHelper.close();        }        public long createSmiley(String source, String info ){            ContentValues initialValues = new ContentValues();            initialValues.put(SOURCE, source);            initialValues.put(INFO, info);            return this.mDb.insert(DATABASE_table, null, initialValues);        }        public boolean deleteSmiley(long rowID) {            return this.mDb.delete(DATABASE_table, ROW_ID + "=" + rowID, null) > 0; //$NON-NLS-1$        }        public Cursor getAllSmileys() {            return this.mDb.query(DATABASE_table, new String[] { ROW_ID,                    SOURCE, INFO }, null, null, null, null, null);        }        public Cursor getSmiley(long rowID) throws sqlException {            Cursor mCursor =            this.mDb.query(true, DATABASE_table, new String[] { ROW_ID, SOURCE,                    INFO }, ROW_ID + "=" + rowID, null, null, null, null, null);            if (mCursor != null) {                mCursor.movetoFirst();            }            return mCursor;        }        public boolean updateSmiley(long rowID, String source, String info,                String cache){            ContentValues args = new ContentValues();            args.put(SOURCE, source);            args.put(INFO, info);            return this.mDb.update(DATABASE_table, args, ROW_ID + "=" + rowID, null) >0;         }    }

我需要在SmileyDBAdapter中更改以使其正常工作.它支持BLOB类型.尝试这个小时后,我真的很沮丧.–

Thx提前为您的答案

最好的祝福

苹果浏览器

解决方法:

检查此代码以从Web链接插入图像并以数据库形式存储在BLOB中并显示在ImageVIEw中.

Java代码:

categoryDe​​tails.Java

public class categoryDetails extends Activity {public NotesDbAdapter mDbHelper;ByteArrayBuffer baf ; private Cursor mNotesCursor;ImageVIEw img;/** Called when the activity is first created. */@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);    mDbHelper = new NotesDbAdapter(this);    mDbHelper.open();    new Task_like_flag().execute();    img=(ImageVIEw)findVIEwByID(R.ID.ImageVIEw01);}public Bitmap convertBlobToBitmap(byte[] blobByteArray) {           Bitmap tempBitmap=null;            if(blobByteArray!=null)    tempBitmap = BitmapFactory.decodeByteArray(blobByteArray, 0, blobByteArray.length);    return tempBitmap;}      public class Task_like_flag extends AsyncTask<String, VoID, VoID> {            private final ProgressDialog dialog = new ProgressDialog(categoryDetails.this);            JsONObject object_Feed;            // can use UI thread here            protected voID onPreExecute() {              this.dialog.setMessage("Loading...");              this.dialog.setCancelable(false);              this.dialog.show();            }            @OverrIDe            protected VoID doInBackground(String... params) {                URL url = null;                try {                    url = new URL("http://www.theblacksheeponline.com/uploaded/Quick_images/681314276069brewehas.jpg");                } catch (MalformedURLException e) {                    // Todo auto-generated catch block                    e.printstacktrace();                }  //http://example.com/image.jpg                //open the connection                URLConnection ucon = null;                try {                    ucon = url.openConnection();                } catch (IOException e) {                    // Todo auto-generated catch block                    e.printstacktrace();                }                //buffer the download                inputStream is = null;                try {                    is = ucon.getinputStream();                } catch (IOException e) {                    // Todo auto-generated catch block                    e.printstacktrace();                }                BufferedinputStream bis = new BufferedinputStream(is,128);                baf = new ByteArrayBuffer(128);                //get the bytes one by one                int current = 0;                try {                    while ((current = bis.read()) != -1) {                            baf.append((byte) current);                    }                } catch (IOException e) {                    // Todo auto-generated catch block                    e.printstacktrace();                }                  mDbHelper.createNote(baf.toByteArray());                return null;            }              @OverrIDe              protected voID onPostExecute(VoID result) {                  byte[] imageByteArray;                  Bitmap theImage = null;                try{                      mNotesCursor = mDbHelper.fetchAllNotes();                      startManagingCursor(mNotesCursor);                    if (mNotesCursor.movetoFirst()) {                      do {                          imageByteArray   =  mNotesCursor.getBlob(mNotesCursor.getColumnIndex(NotesDbAdapter.KEY_IMAGE));                          ByteArrayinputStream imagestream = new ByteArrayinputStream(imageByteArray);                          theImage= BitmapFactory.decodeStream(imagestream);                      } while (mNotesCursor.movetoNext());                  }                }catch(Exception e){                  Log.v("Excep", ""+e);                }                img.setimageBitmap(theImage);                  if (this.dialog.isShowing()) {                      this.dialog.dismiss();                    }              }            }  }

NotesDbAdapter.Class

public class NotesDbAdapter {public static final String KEY_category = "category";public static final String KEY_DATE = "notes_date";public static final String KEY_DESC = "item_desc";public static final String KEY_PRIZE = "item_prize";public static final String KEY_MODE = "mode";public static final String KEY_MONTH = "month";public static final String KEY_IMAGE = "img";public static final String KEY_ROWID = "_ID";private static final String TAG = "NotesDbAdapter";private DatabaseHelper mDbHelper;private sqliteDatabase mDb;/** * Database creation sql statement */private static final String DATABASE_CREATE =        "create table notes (_ID integer primary key autoincrement, "                + "img BLOB not null);";private static final String DATABASE_name = "data";private static final String DATABASE_table = "notes";private static final int DATABASE_VERSION = 2;private final Context mCtx;private static class DatabaseHelper extends sqliteOpenHelper {    DatabaseHelper(Context context) {        super(context, DATABASE_name, null, DATABASE_VERSION);    }    @OverrIDe    public voID onCreate(sqliteDatabase db) {        db.execsql(DATABASE_CREATE);    }    @OverrIDe    public voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "                + newVersion + ", which will destroy all old data");        db.execsql("DROP table IF EXISTS notes");        onCreate(db);    }}/** * Constructor - takes the context to allow the database to be * opened/created *  * @param ctx the Context within which to work */public NotesDbAdapter(Context ctx) {    this.mCtx = ctx;}/** * Open the notes database. If it cannot be opened, try to create a new * instance of the database. If it cannot be created, throw an exception to * signal the failure *  * @return this (self reference, allowing this to be chained in an *         initialization call) * @throws sqlException if the database Could be neither opened or created */public NotesDbAdapter open() throws sqlException {    mDbHelper = new DatabaseHelper(mCtx);    mDb = mDbHelper.getWritableDatabase();    return this;}public voID close() {    mDbHelper.close();}/** * Create a new note using the Title and body provIDed. If the note is * successfully created return the new rowID for that note, otherwise return * a -1 to indicate failure. *  * @param Title the Title of the note * @param body the body of the note * @return rowID or -1 if Failed */public long createNote(byte[] img) {     byte yes[]=img;    ContentValues initialValues = new ContentValues();    initialValues.put(KEY_IMAGE, yes);    Log.v("row", ""+mDb.insert(DATABASE_table, null, initialValues));    return mDb.insert(DATABASE_table, null, initialValues);}/** * Return a Cursor over the List of all notes in the database *  * @return Cursor over all notes */public Cursor fetchAllNotes() {    return mDb.query(DATABASE_table, null, null, null, null, null, null);} }

Main.xml:

<?xml version="1.0" enCoding="utf-8"?>   <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:orIEntation="vertical"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent"   >  <ImageVIEw        androID:ID="@+ID/ImageVIEw01"       androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:src="@drawable/icon"/> </linearLayout>

如果您发现任何难度,请告诉我.

谢谢Venky ..

总结

以上是内存溢出为你收集整理的android – 如何在DBAdapter中创建一个类型为BLOB的列的表全部内容,希望文章能够帮你解决android – 如何在DBAdapter中创建一个类型为BLOB的列的表所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1101858.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存