SQLiteOpenHelper 打开自定义路径下的db数据库文件

       最近在做一个安卓项目,项目使用的sqlite数据库是cocos2dx 创建的,放在了/data/data/com.*.*/files下面,而SQLiteOpenHelper打开的文件默认是放在/data/data/databases/下面的,SQLiteOpenHelper里面没有地方能改变数据库的路径。于是在网上查阅了国外大牛的解决方案,完美解决了我的问题。解决办法:

       自定义Context,该Context用来给SQLiteOpenHelper初始化的时候使用,该类是:

       

import java.io.File;

import org.cocos2dx.lua.AppActivity;

import android.content.Context;
import android.content.ContextWrapper;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

class DatabaseContext extends ContextWrapper {

	private static final String DEBUG_CONTEXT = "DatabaseContext";

	public DatabaseContext(Context base) {
		super(base);
	}

	@Override
	public File getDatabasePath(String name) {
		File sdcard = Environment.getExternalStorageDirectory();
		String dbfile = AppActivity.getWriteablePath() + name;
		if (!dbfile.endsWith(".db")) {
			dbfile += ".db";
		}

		File result = new File(dbfile);

		if (!result.getParentFile().exists()) {
			result.getParentFile().mkdirs();
		}

		return result;
	}

	/*
	 * this version is called for android devices >= api-11. thank to @damccull
	 * for fixing this.
	 */
	@Override
	public SQLiteDatabase openOrCreateDatabase(String name, int mode,
			SQLiteDatabase.CursorFactory factory,
			DatabaseErrorHandler errorHandler) {
		return openOrCreateDatabase(name, mode, factory);
	}

	/* this version is called for android devices < api-11 */
	@Override
	public SQLiteDatabase openOrCreateDatabase(String name, int mode,
			SQLiteDatabase.CursorFactory factory) {
		SQLiteDatabase result = SQLiteDatabase.openOrCreateDatabase(
				getDatabasePath(name), null);

		return result;
	}
}

使用方法是在自定义的DBHelper类构造函数里面传一下上面的自定义的ContextWrapper即可

public class DBHelper extends SQLiteOpenHelper {

	public DBHelper(Context context){
		
		super(new DatabaseContext(context), "nbg_bbm.db", null, 1);
	}
	
	@Override
	public void onCreate(SQLiteDatabase dbDatabase){
		dbDatabase.execSQL("create table download_info(_id integer PRIMARY KEY AUTOINCREMENT, thread_id integer, "
				
				+"start_pos integer, end_pos integer, compelete_size integer, url char,file_size integer, pid integer,fc_id integer,logo_url char, title char, desc char)");
		
		dbDatabase.execSQL("create table file_info(_id integer PRIMARY KEY AUTOINCREMENT, title char, "
				+"kind char, file_size char,url char, pid integer,fc_id integer,logo_url char, desc char)");
		
		dbDatabase.execSQL("create table course_info(_id integer PRIMARY KEY AUTOINCREMENT,pid integer,fc_id integer, sc_id char, name char, scene char, y_level integer,w_level integer, m_level integer,passtype integer,bid integer,"+
				" bnum integer, kidx integer, recommend integer, title char, lib_ver integer, descp char, version char )");
		
		dbDatabase.execSQL("CREATE TABLE IF NOT EXISTS nbg_babyinfo_new(_id integer PRIMARY KEY AUTOINCREMENT,nameaudio INTEGER , name text , nickname text ,restLimit INTEGER , sex INTEGER , birth INTEGER , getup INTEGER , sleep INTEGER , rest INTEGER , max_pid1 INTEGER, max_pid2 INTEGER, max_pid3 INTEGER, max_pid4 INTEGER, max_pid5 INTEGER, max_pid6 INTEGER)");
	}

	
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}
	

}


你可能感兴趣的:(安卓)