LitePal導(dǎo)入外部(assets文件夾下)的數(shù)據(jù)庫

之前做一個(gè)考試的項(xiàng)目,有個(gè)需求是 點(diǎn)擊考試按鈕進(jìn)入考試,然后查詢數(shù)據(jù)庫獲取題目,數(shù)據(jù)庫是本地已經(jīng)有的,由于我用的是LitePal,剛接觸這個(gè)不知道怎么查詢到外部的數(shù)據(jù)庫。然后苦思了幾天(由于還比較菜,請?jiān)徫业男剩┙鉀Q了這個(gè)問題。

以下是解決思路:
LitePal的使用方法就不說了,網(wǎng)上找一大堆,這里是先在本地創(chuàng)建一個(gè)同樣名字(不同名也行)的數(shù)據(jù)庫,把需要用到的表創(chuàng)建出來。
怎么創(chuàng)建就不把代碼貼出來了,直接上有用的代碼:

public class DBHelper {

Context context;
private SQLiteDatabase db;

//數(shù)據(jù)庫的名稱
private String DB_NAME = "title.db";
//數(shù)據(jù)庫的地址
private String DB_PATH = "/data/data/包名/databases/";

public DBHelper(Context context){
    this.context = context;
    initFile();
    db = SQLiteDatabase.openDatabase("/data/data/包名/databases/title.db",
            null, SQLiteDatabase.OPEN_READWRITE);
    DataSupport.saveAll(dbHelper.getPDQuestion());//把查到的數(shù)據(jù)保存到LitePal中,方便使用查詢
}
// 獲取數(shù)據(jù)庫中的表(這里只寫入了一張表)
public List<PDSubject> getPDQuestion() {
    List<PDSubject> list = new ArrayList<>();
    //執(zhí)行sql語句
    Cursor cursor = db.rawQuery("select * from PDTest", null);
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        int count = cursor.getCount();
        //遍歷
        for (int i = 0; i < count; i++) {
            cursor.moveToPosition(i);
            PDSubject pdBean = new PDSubject();
            pdBean.setMain_title(cursor.getString(cursor.getColumnIndex("MainTitle")));//題目內(nèi)容
            pdBean.setA(cursor.getString(cursor.getColumnIndex("A")));//A答案
            pdBean.setB(cursor.getString(cursor.getColumnIndex("B")));//B答案
            pdBean.setAnswer(cursor.getString(cursor.getColumnIndex("Answer")));//正確答案
            pdBean.setMain_title_len(cursor.getInt(cursor.getColumnIndex("MaintitleLen")));//題目的長度
            list.add(pdBean);
        }
    }
    return list;
}

private void initFile(){
    //判斷數(shù)據(jù)庫是否拷貝到相應(yīng)的目錄下
    if (new File(DB_PATH + DB_NAME).exists() == false) {
        File dir = new File(DB_PATH);
        if (!dir.exists()) {
            dir.mkdir();
        }
        //復(fù)制文件
        try {
            InputStream is = context.getAssets().open(DB_NAME);
            OutputStream os = new FileOutputStream(DB_PATH + DB_NAME);
            byte[] buffer = new byte[1024];//用來復(fù)制文件
            int length;//保存已經(jīng)復(fù)制的長度
            //開始復(fù)制
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
            //刷新
            os.flush();
            //關(guān)閉
            os.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
         }
     }
 }
}

創(chuàng)建一個(gè)DBHelper類把a(bǔ)ssets文件夾下的數(shù)據(jù)庫文件寫入到系統(tǒng)默認(rèn)的database文件夾下,然后通過查詢當(dāng)前數(shù)據(jù)庫把數(shù)據(jù)放入List中。

然后再程序的首個(gè)activity或Application中執(zhí)行DBHelper的構(gòu)造方法
這里我只復(fù)制了一張表,可以用同樣的方法把所有表寫入進(jìn)去(有點(diǎn)麻煩,而且這樣在你的程序中應(yīng)該就會有三個(gè)一樣數(shù)據(jù)庫了。。。但是暫時(shí)我還沒想到有什么別的方法)
執(zhí)行完上面的代碼后就可以用LitePal的查詢方法了

    PDSubject dest = DataSupport.findFirst(PDSubject.class);

好了,大功告成?。?!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容