public abstract class ToSDCardSQLiteOpenHelper {



private static final String TAG = ToSDCardSQLiteOpenHelper.class.getSimpleName();

 

private final Context mContext;
private final String mName;
private final String mPath;//要放到SDCard下的資料夾路徑
private final CursorFactory mFactory;
private final int mNewVersion;

 

private SQLiteDatabase mDatabase = null;
private boolean mIsInitializing = false;

 

public ToSDCardSQLiteOpenHelper(Context context,String path,String name, CursorFactory factory, int version) {
if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);

 

mContext = context;

 

mPath=path;
mName = name;
mFactory = factory;
mNewVersion = version;
}

 

public synchronized SQLiteDatabase getWritableDatabase() {
if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
return mDatabase; // The database is already open for business
}

 

if (mIsInitializing) {
throw new IllegalStateException("getWritableDatabase called recursively");
}

 

boolean success = false;
SQLiteDatabase db = null;
try {
mIsInitializing = true;
if (mName == null) {
db = SQLiteDatabase.create(null);
} else {
String path = getDatabasePath(mPath,mName).getPath();
db = SQLiteDatabase.openOrCreateDatabase(path,mFactory);
}

 

int version = db.getVersion();
if (version != mNewVersion) {
db.beginTransaction();
try {
if (version == 0) {
onCreate(db);
} else {
onUpgrade(db, version, mNewVersion); db.setVersion(mNewVersion);
db.setTransactionSuccessful();
} finally {
db.endTransaction(); }

 

onOpen(db);
success = true;
return db;
} finally {
mIsInitializing = false;
if (success) {
if (mDatabase != null) {
try { mDatabase.close(); } catch (Exception e) { } mDatabase = db;
} else {
if (db != null) db.close(); }
}

 

public synchronized SQLiteDatabase getReadableDatabase() {
if (mDatabase != null && mDatabase.isOpen()) {
return mDatabase; // The database is already open for business
}

 

if (mIsInitializing) {
throw new IllegalStateException("getReadableDatabase called recursively");
}

 

try {
return getWritableDatabase();
} catch (SQLiteException e) {
if (mName == null) throw e; // Can't open a temp database read-only!
Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
}

 

SQLiteDatabase db = null;
try {
mIsInitializing = true;
String path = getDatabasePath(mPath,mName).getPath();
db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READWRITE);
if (db.getVersion() != mNewVersion) {
throw new SQLiteException("Can't upgrade read-only database from version " +
db.getVersion() + " to " + mNewVersion + ": " + path);
}

 

onOpen(db);
Log.w(TAG, "Opened " + mName + " in read-only mode");
mDatabase = db;
return mDatabase;
} finally {
mIsInitializing = false;
if (db != null && db != mDatabase) db.close(); }



public synchronized void close() {
if (mIsInitializing) throw new IllegalStateException("Closed during initialization");

 

if (mDatabase != null && mDatabase.isOpen()) {
mDatabase.close();
mDatabase = null; }

 

public abstract void onCreate(SQLiteDatabase db);

 

public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
public void onOpen(SQLiteDatabase db) {}



public File getDatabasePath(String dbpath,String dbName) if (dbpath==null) {
dbpath="";
}
File path = new File(Environment.getExternalStorageDirectory(),dbpath);//創建目錄
File f = new File(path.getPath(),dbName);// 創建檔
if (!path.exists()) {// 目錄存在返回false
path.mkdirs();// 創建一個目錄
}

 

if (!f.exists()) {// 檔存在返回false
try {
f.createNewFile();//創建檔
} catch (IOException e) {
e.printStackTrace(); }
return f;
}
}
 
 
--------------------------------------------------------------------------------------------

在使用時,創建一個類繼承以上類即可,使用方法與繼承SQLiteOpenHelper 一樣。只是可以多一個

資料夾路徑而已,如果不想創建資料夾,則在構造函數中把 DIR 賦為null即可。

public class DBOpenHelper extends ToSDCardSQLiteOpenHelper {

private static final String DIR="wqx";
private static final String NAME="words.db";

private static final int VERSION=1;

public DBOpenHelper(Context context) {
super(context, DIR,NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table word("wordId integer primary key,indexWord varchar(10)));
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists word");
onCreate(db);
}

---------------------------------------------------------------------------------

另外,還要在設定檔中加入SDCard相關許可權: <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在具體程式中判斷是否存在SDCard

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)==true){

....
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()