天天看點

如何在SD卡中建立資料庫

  (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/