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);
}
@Override
public 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 tableName
public Cursor query(String tableName) {
db = getWritableDatabase();
Cursor c = db.query(tableName, null, null, null, null, null, null);
return c;
}
// Return cursor by SQL string
public 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 query
public void execSQL(String sql) {
db = getWritableDatabase();
db.execSQL(sql);
}
// Delete by id
public 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();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Bitmap to byte[]
public byte[] bmpToByteArray(Bitmap bmp) {
// Default size is 32 bytes
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return bos.toByteArray();
}
// Cursor to bitmap
Bitmap cursorToBmp(Cursor c, int columnIndex) {
byte[] data = c.getBlob(columnIndex);
try {
return BitmapFactory.decodeByteArray(data, 0, data.length);
} catch (Exception e) {
return null;
}
}
}
DBhelper调用方法:
//定义helper
private 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"));
分享到:
相关推荐
数据库的使用,包含增删改查,是使用android 内置的数据库助手类实现的
Android高级编程雪梨作业之自定义ContentProvider 将任务01生词本作业中生成的生词本数据库通过自定义ContentProvider的方式,共享给其他应用。 要求如下: (1) 使用自定义SQLiteOpenHelper来管理数据库; (2) 提交...
android SQLiteOpenHelper 数据库简单应用,今天闲着没事自己写的demo
数据库助手示例这是一个 Android DBHelper 示例,它扩展了 SQLiteOpenHelper
android的SQLiteOpenHelper类的应用实例.docx
在android studio中存储数据有三个方法,分别是: (1)简单存储——SharedPreferences (2)文件存储: 内部存储——应用程序私有文件 外部存储——SD卡 资源文件——只读( RAW 、XML) (3)数据库存储——...
在android中常用存储数据的基本就三种,sqlite,SharedPreferences,文件存储,其中针对于对象存储,使用sqlite比较多,因为可以对其进行增删改查。本文主要讲解SQLiteOpenHelper的封装使用,代码引用自...
SQLiteOpenHelper重点在于helper,他是SQLiteDatabase的一个帮助类,便于开发者实现对SQLite的数据进行写入(增删改)和读取(查询)。 2. SQLiteOpenHelper具体有什么方法? 使用SQLiteOpenHelper,便是构建一个类...
NULL 博文链接:https://davidhhs.iteye.com/blog/2150780
NULL 博文链接:https://byandby.iteye.com/blog/836250
1.建立DbHelper类: ...import android.database.sqlite.SQLiteOpenHelper; public class DbHelper extends SQLiteOpenHelper { public DbHelper(Context context, String name, CursorFactory factory,
主要介绍了Android开发之Sqliteopenhelper用法,实例分析了SQLiteOpenHelper类操作数据库的相关技巧,需要的朋友可以参考下
android 手机数据库 实现SQLiteOpenHelper
068集-SQLiteOpenHelper(2)视频教程,个人感觉还可以,共享给大家。
android项目添加数据库,利用SQLiteOpenHelper创建数据库文件的Demo
在android studio中用SQLiteOpenHelper()方法建立数据库 注:这篇文章是承接上面发的链接的文章,有什么问题欢迎讨论 用SQLiteOpenHelper()方法创建表结构可以用onCreat()函数和onUpgrade()函数 一.用...
在android studio中存储数据有三个方法,分别是: (1)简单存储——SharedPreferences (2)文件存储:内部存储——应用程序私有文件外部存储——SD卡资源文件——只读( RAW 、XML) (3)数据库存储——...
对Android数据库SQlite的基本操作,打开或新建数据库,增,删,改,查。 SQLiteOpenHelper类的运用。 ListView的运用和适配器的使用。 Menu键的使用。
(代码模板)新建一个SQLiteOpenHelper助手类.xml