(1)数据库创建在内存卡中,大小受限,创建位置位于/data/data/应用程序名/databases中(可使用eclispe的ddms查看)。
(2)如果无法获取root权限,则无法直接查看创建的数据库。
if (mname == null) {
db = sqlitedatabase.create(null);
}
else {
db = mcontext.openorcreatedatabase(mname, 0, mfactory);
分析上述代码发现,当数据库名字为非空时,创建数据库或打开由mcontext完成,这个mcontext由sqliteopenhelper的构造函数传入:sqliteopenhelper(context context, string name, sqlitedatabase.cursorfactory factory, int version)。那么我们对于传入的context,重载其openorcreatedatabase函数,使其将数据库创建到sd卡中就可完成我们的目标了~。
对应的sqliteopenhelper实现类sdcarddbhelper
import android.content.context;
import android.database.sqlexception;
import android.database.sqlite.sqlitedatabase;
import android.database.sqlite.sqliteopenhelper;
import android.util.log;
/**
* 数据库管理和维护类
**/
public class sdcarddbhelper extends sqliteopenhelper{
public static final string tag = "sdcarddbhelper";
* 数据库名称
public static string database_name = "sddb.db";
* 数据库版本
public static int database_version = 1;
* 构造函数
*
* @param context 上下文环境
public sdcarddbhelper(context context){
super(context, database_name, null, database_version);
}
* 创建数据库时触发,创建离线存储所需要的数据库表
* @param db
@override
public void oncreate(sqlitedatabase db) {
log.e(tag, "开始创建数据库表");
try{
//创建用户表(user)
db.execsql("create table if not exists user" +
"(_id integer primary key autoincrement,name varchar(20),password varchar(20),role varchar(10),updatetime varchar(20))");
log.e(tag, "创建离线所需数据库表成功");
catch(sqlexception se){
se.printstacktrace();
log.e(tag, "创建离线所需数据库表失败");
/** 更新数据库时触发,
* @param oldversion
* @param newversion
public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {
//db.execsql("alter table person add column other string");
重载的openorcreatedatabase在sd卡上创建数据库的context
import java.io.file;
import java.io.ioexception;
import android.content.contextwrapper;
import android.database.databaseerrorhandler;
import android.database.sqlite.sqlitedatabase.cursorfactory;
* 用于支持对存储在sd卡上的数据库的访问
public class databasecontext extends contextwrapper {
* @param base 上下文环境
*/
public databasecontext(context base){
super(base);
* 获得数据库路径,如果不存在,则创建对象对象
* @param name
* @param mode
* @param factory
public file getdatabasepath(string name) {
//判断是否存在sd卡
boolean sdexist = android.os.environment.media_mounted.equals(android.os.environment.getexternalstoragestate());
if(!sdexist){//如果不存在,
log.e("sd卡管理:", "sd卡不存在,请加载sd卡");
return null;
else{//如果存在
//获取sd卡路径
string dbdir=android.os.environment.getexternalstoragedirectory().getabsolutepath();
dbdir += "/database";//数据库所在目录
string dbpath = dbdir+"/"+name;//数据库路径
//判断目录是否存在,不存在则创建该目录
file dirfile = new file(dbdir);
if(!dirfile.exists())
dirfile.mkdirs();
//数据库文件是否创建成功
boolean isfilecreatesuccess = false;
//判断文件是否存在,不存在则创建该文件
file dbfile = new file(dbpath);
if(!dbfile.exists()){
try {
isfilecreatesuccess = dbfile.createnewfile();//创建文件
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
else
isfilecreatesuccess = true;
//返回数据库文件对象
if(isfilecreatesuccess)
return dbfile;
* 重载这个方法,是用来打开sd卡上的数据库的,android 2.3及以下会调用这个方法。
public sqlitedatabase openorcreatedatabase(string name, int mode,
sqlitedatabase.cursorfactory factory) {
sqlitedatabase result = sqlitedatabase.openorcreatedatabase(getdatabasepath(name), null);
return result;
* android 4.0会调用此方法获取数据库。
* @see android.content.contextwrapper#openorcreatedatabase(java.lang.string, int,
* android.database.sqlite.sqlitedatabase.cursorfactory,
* android.database.databaseerrorhandler)
* @param errorhandler
public sqlitedatabase openorcreatedatabase(string name, int mode, cursorfactory factory,
databaseerrorhandler errorhandler) {
调用程序:
databasecontext dbcontext = new databasecontext(this);
sdcarddbhelper dbhelper = new sdcarddbhelper(dbcontext);
这里尤其值得注意的是,不同版本的android api会调用不同的openorcreatedatabase函数。
当然也可直接使用sqlitedatabase创建sd卡上的数据库,或者直接修改sqliteopenhelper的源码重新编译,不过前者没有对数据库进行一些检验容错处理,也不及sqliteopenhelper对数据库操作方便。后者工作量较大,不建议采用。
最后注意记得加入对sd卡的读写权限:
<uses-permission android:name="android.permission.write_external_storage"></uses-permission>
最新内容请见作者的github页:http://qaseven.github.io/