2012-04-26 95 views
5

過去幾天我一直在試圖複製資產文件夾中的兩個數據庫,但無法取得任何成功。在Android中的資產文件夾中複製兩個SQLite數據庫

我設法複製並訪問它的一個數據庫。但第二個,我需要你的幫助。

+1

第二個數據庫的問題是什麼?你爲什麼不執行與複製第一個步驟相同的步驟..? – Mudassir 2012-04-26 12:40:51

+0

您應該添加一些更多信息,例如失敗的代碼和錯誤的logcat輸出。無法查看代碼中出現了什麼問題。 – zapl 2012-04-26 12:41:01

+2

@Mudassir我可以使用第二個相同的數據庫輔助類..? – 2012-04-26 17:20:08

回答

4
private void copydatabase() throws IOException { 
//Open your local db as the input stream 
InputStream myinput = mycontext.getAssets().open(DB_NAME);// Path to the just created empty db 
String outfilename = DB_PATH + DB_NAME; 
//Open the empty db as the output stream 
OutputStream myoutput = new FileOutputStream("/data/data/(packagename)/databases /(datbasename).sqlite"); 
// transfer byte to inputfile to outputfile 
byte[] buffer = new byte[1024]; 
int length; 
while ((length = myinput.read(buffer))>0) 
{ 
myoutput.write(buffer,0,length); 
} 
//Close the streams 
myoutput.flush(); 
myoutput.close(); 
myinput.close(); 
} 

爲您的第二個數據庫執行此操作。

相關問題