Android 继承SQLiteOpenHelper自定义DBHelper存取数据与图像如下:

package com.test;import java.io.ByteArrayOutputStream;import java.io.IOException;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class DBHelper extends SQLiteOpenHelper {private static final String DB_NAME = "test.db";private static final int DB_VERSION = 1;private static final String TABLE_NAME = "info";private static final String CREATE_INFO = "create table if not exists info("+ "id integer primary key autoincrement,name varchar(20),"+ "time varchar(20),img BLOB)";private SQLiteDatabase db;DBHelper(Context c) {//super(c, DB_NAME, null, DB_VERSION);}@Overridepublic void onCreate(SQLiteDatabase db) {this.db = db;db.execSQL(CREATE_INFO);}public void insert(ContentValues values, String tableName) {db = getWritableDatabase();db.insert(tableName, null, values);db.close();}// Return cursor with all columns by tableNamepublic Cursor query(String tableName) {db = getWritableDatabase();Cursor c = db.query(tableName, null, null, null, null, null, null);return c;}// Return cursor by SQL stringpublic Cursor rawQuery(String sql, String[] args) {db = getWritableDatabase();Cursor c = db.rawQuery(sql, args);return c;}// Execute a single SQL statement(as insert,create,delete)instead of a querypublic void execSQL(String sql) {db = getWritableDatabase();db.execSQL(sql);}// Delete by idpublic void del(int id) {if (db == null)db = getWritableDatabase();db.delete(TABLE_NAME, "id=?", new String[] { String.valueOf(id) });}public void close() {if (db != null)db.close();}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}// Bitmap to byte[]public byte[] bmpToByteArray(Bitmap bmp) {// Default size is 32 bytesByteArrayOutputStream bos = new ByteArrayOutputStream();try {bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);bos.close();} catch (IOException e) {e.printStackTrace();}return bos.toByteArray();}// Cursor to bitmapBitmap cursorToBmp(Cursor c, int columnIndex) {byte[] data = c.getBlob(columnIndex);try {return BitmapFactory.decodeByteArray(data, 0, data.length);} catch (Exception e) {return null;}}}

DBhelper调用方法:

//定义helperprivate static DBHelper helper;//创建helper helper = new DBHelper(this);//插入数据与图像ContentValues values = new ContentValues();values.put("name", "test");values.put("img", helper.bmpToByteArray(bmp));helper.insert(values, "info");//访问数据与图像Cursor c = helper.rawQuery("select * from info", null);c.moveToLast();String name = c.getString(c.getColumnIndex("name"));Bitmap bmp = cursorToBmp(c, c.getColumnIndex("img"));






更多相关文章

  1. Android(安卓)Shareperferences使用
  2. android mvvm livedata_一文搞懂Android(安卓)JetPack组件原理之
  3. Android天气预报详解
  4. 2011Android技术面试整理附有详细答案(包括百度、新浪、中科软等
  5. 【Android的从零单排开发日记】之入门篇(七)——Android数据存储(上
  6. Android(四)数据存储之四网络
  7. [数据图表]Android军团超级黑马亚马逊平板电脑Amazon Kindle Fir
  8. android 数据储存——网络存储(5)
  9. android与html5的交互——数据库操作,UI操作,以及html5的localStor

随机推荐

  1. Android中JNI实现
  2. android map
  3. Android(安卓)LayoutInflater.inflate(R.
  4. android 通过广播获取指定联系人短信内容
  5. Android(安卓)MTK Launcher3 替换桌面图
  6. 2013.12.04 (4)——— android SlidingMenu
  7. Android开源项目
  8. 解决Flutter 编译异常path_provider
  9. Android应用程序键盘(Keyboard)消息处理机
  10. Android中直接按路径读取properties文件