2011-07-22 98 views
3

我在android中做一個應用程序,我的系統中有.sqlite文件,名爲'ff.sqlite'。在.sqlite文件中,我有10個表,非常大的表記錄。在我的應用程序(android)中,我將.sqlite文件放在原始文件夾中,並創建了一個名爲'festival.db'的數據庫。現在我不知道如何將ff.sqlite文件導入到festival.db中。請幫幫我。如何將.sqlite導入到android上的sqlite中的數據庫

res/raw/ff.sqlite 

創建數據庫,我用下面的代碼在android系統:

 SQLiteDatabase dbs; 
    dbs = openOrCreateDatabase("festival.db", SQLiteDatabase.CREATE_IF_NECESSARY, null); 

我的問題是,我需要從.sqlite文件中的festival.db所有表,請幫助我。

回答

6

從資產文件夾中的DB文件複製到內存中,然後打開數據庫,並從那裏做CRUD:

private void copyDB() throws IOException{ 

     InputStream myInput = m_Context.getAssets().open(DB-SQLITE_FILE_NAME_KEPT_IN_ASSET_FOLDER); 
     String outFileName = "/data/data/your.package.name/databases/"; 
     OutputStream myOutput = new FileOutputStream(outFileName); 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myInput.read(buffer))>0){ 
      myOutput.write(buffer, 0, length); 
     } 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 

    } 
+1

感謝這裏給.sqllite文件名? –

相關問題