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

来源:互联网 发布:ip变换软件 编辑:程序博客网 时间:2024/05/01 09:57


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

[java] view plaincopy
  1. package com.test;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5.   
  6. import android.content.ContentValues;  
  7. import android.content.Context;  
  8. import android.database.Cursor;  
  9. import android.database.sqlite.SQLiteDatabase;  
  10. import android.database.sqlite.SQLiteOpenHelper;  
  11. import android.graphics.Bitmap;  
  12. import android.graphics.BitmapFactory;  
  13.   
  14. public class DBHelper extends SQLiteOpenHelper {  
  15.   
  16.     private static final String DB_NAME = "test.db";  
  17.     private static final int DB_VERSION = 1;  
  18.     private static final String TABLE_NAME = "info";  
  19.     private static final String CREATE_INFO = "create table if not exists info("  
  20.             + "id integer primary key autoincrement,name varchar(20),"  
  21.             + "time varchar(20),img BLOB)";  
  22.     private SQLiteDatabase db;  
  23.   
  24.     DBHelper(Context c) {  
  25.         //  
  26.         super(c, DB_NAME, null, DB_VERSION);  
  27.     }  
  28.   
  29.     @Override  
  30.     public void onCreate(SQLiteDatabase db) {  
  31.         this.db = db;  
  32.         db.execSQL(CREATE_INFO);  
  33.     }  
  34.   
  35.     public void insert(ContentValues values, String tableName) {  
  36.         db = getWritableDatabase();  
  37.         db.insert(tableName, null, values);  
  38.         db.close();  
  39.     }  
  40.   
  41.     // Return cursor with all columns by tableName  
  42.     public Cursor query(String tableName) {  
  43.         db = getWritableDatabase();  
  44.         Cursor c = db.query(tableName, nullnullnullnullnullnull);  
  45.         return c;  
  46.     }  
  47.   
  48.     // Return cursor by SQL string  
  49.     public Cursor rawQuery(String sql, String[] args) {  
  50.         db = getWritableDatabase();  
  51.         Cursor c = db.rawQuery(sql, args);  
  52.         return c;  
  53.     }  
  54.   
  55.     // Execute a single SQL statement(as insert,create,delete)instead of a query  
  56.     public void execSQL(String sql) {  
  57.         db = getWritableDatabase();  
  58.         db.execSQL(sql);  
  59.     }  
  60.   
  61.     // Delete by id  
  62.     public void del(int id) {  
  63.         if (db == null)  
  64.             db = getWritableDatabase();  
  65.         db.delete(TABLE_NAME, "id=?"new String[] { String.valueOf(id) });  
  66.     }  
  67.   
  68.     public void close() {  
  69.         if (db != null)  
  70.             db.close();  
  71.     }  
  72.   
  73.     @Override  
  74.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  75.     }  
  76.   
  77.     // Bitmap to byte[]  
  78.     public byte[] bmpToByteArray(Bitmap bmp) {  
  79.         // Default size is 32 bytes  
  80.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  81.         try {  
  82.             bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);  
  83.             bos.close();  
  84.         } catch (IOException e) {  
  85.             e.printStackTrace();  
  86.         }  
  87.         return bos.toByteArray();  
  88.     }  
  89.   
  90.     // Cursor to bitmap  
  91.     Bitmap cursorToBmp(Cursor c, int columnIndex) {  
  92.   
  93.         byte[] data = c.getBlob(columnIndex);  
  94.         try {  
  95.             return BitmapFactory.decodeByteArray(data, 0, data.length);  
  96.         } catch (Exception e) {  
  97.             return null;  
  98.         }  
  99.     }  
  100.   
  101. }  

DBhelper调用方法:

[java] view plaincopy
  1. //定义helper  
  2. private static DBHelper helper;  
  3. //创建helper  
  4.  helper = new DBHelper(this);  
  5. //插入数据与图像  
  6. ContentValues values = new ContentValues();  
  7. values.put("name""test");  
  8. values.put("img", helper.bmpToByteArray(bmp));  
  9. helper.insert(values, "info");  
  10. //访问数据与图像  
  11. Cursor c = helper.rawQuery("select * from info"null);  
  12. c.moveToLast();  
  13. String name = c.getString(c.getColumnIndex("name"));  
  14. Bitmap bmp = cursorToBmp(c, c.getColumnIndex("img"));  
0 0