1. 程式人生 > >Android讀取外部儲存裝置(自帶SD卡及外部接入儲存裝置 )中的資料庫檔案

Android讀取外部儲存裝置(自帶SD卡及外部接入儲存裝置 )中的資料庫檔案

 我們在開發工作中,時時刻刻在與資料打交道,儲存資料的方式很多,其中使用資料庫來儲存及操作資料是最常見的一種方式。
 Android內嵌了SQLite資料庫,這是一種非常流行的嵌入式及輕型關係型資料庫。支援SQL,支援多種作業系統,完全獨立執行,沒有依賴性。
 我們在開發中建立的資料庫檔案預設路徑是:/data/data/專案包名/資料庫名。但是有時候,我們需要操作並不在此路徑下的資料庫檔案。
 比如我們有時候會把資料庫檔案放到res/raw目錄下,同項目一起打包釋出。在res/raw目錄下的檔案即不在預設的路徑下。
 通常我們是要先把assert下檔案複製到指定檔案路徑 ,再操作資料庫,這樣即可。
 現在要記錄的和上面的都不太一樣,我們就把資料庫直接放置到SD卡(或其它儲存裝置)中指定目錄,然後直接對該資料庫進行讀取操作。
 通常我們操作資料庫是自己寫個類extends SQLiteOpenHelper ,然後呼叫對應的方法操作資料庫:
public class TestDBHelper extends SQLiteOpenHelper {  
    private static final int VERSION = 1;  

    /** 
     * 在SQLiteOpenHelper的子類當中,必須有該建構函式 
     */  
    public DatabaseHelper(Context context, String name, CursorFactory factory,  int version) { 
        super(context, name, factory, version);  
    }  


     /** 
     * 第一次建立時執行,通常在這裡建立表格
     */
@Override public void onCreate(SQLiteDatabase db) { // CREATE TABLE..... } /** * 資料庫升級時執行,升級需要改上面對應版本號 */ @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { } }

但是在讀寫外接資料庫時,使用到的是SQLiteDatabase.openOrCreateDatabase(file, factory)方法,第一個引數指的就是資料庫檔案,第二個引數通常傳入null即可。直接上個簡單的程式碼:

public class CustomSQLTools {
    //資料庫儲存路徑
    private String filePath;
    private SQLiteDatabase database;

    public SQLiteDatabase openDatabase(Context context) {
        File jhPath = new File(filePath);
        //檢視資料庫檔案是否存在
        if (jhPath.exists()) {
            //存在則直接返回開啟的資料庫
            return SQLiteDatabase.openOrCreateDatabase(jhPath, null);
        } else {
            Toast.makeText(context, "資料庫檔案不存在", Toast.LENGTH_SHORT).show();
            return null;
        }
    }
}

在Activity中使用:

public class TestDbActivity extends BaseAct {
    private TextView test_db;
    @Override
    public void setRootView() {
        setContentView(R.layout.activity_test);
        test_db = (TextView) findViewById(R.id.test_db);
        test_db.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        if (v.getId() == R.id.test_db) {
        //點選直接根據條件查詢資料庫資料
            initSqlAndQuery("1000001739", date);
            });
        }
    }


    private void initSqlAndQuery(String queryId, String queryDate) {
        List<TestDataBean> queryResultList = new ArrayList<>();
        CustomSQLTools s = new CustomSQLTools();
        SQLiteDatabase database = s.openDatabase(getApplicationContext());
        String sql = "select * from VITAL_DATA_BEAN where deviceId = ? and date like?";
        Cursor cursor = database.rawQuery(sql, new String[]{queryId, queryDate + "%"});

        while (cursor.moveToNext()) {
            TestDataBeanbean = new TestDataBean();
            int deviceId = cursor.getInt(cursor.getColumnIndex("deviceId"));
            String date = cursor.getString(cursor.getColumnIndex("date"));
            String rpm = cursor.getString(cursor.getColumnIndex("rpm"));
            String movementFast = cursor.getString(cursor.getColumnIndex("movementFast"));
            //設定對應實體資料,從而獲取到我們需要的資料
            bean.setDeviceId(deviceId);
            bean.setDate(date);
            bean.setRpm(rpm);
            bean.setMovementFast(movementFast);
            queryResultList.add(bean);
        }
        cursor.close();
        database.close();
    }
}