2011-09-17 71 views
1

我使用SQLite數據庫(300 KB)傳送我的Android應用程序,在讀完之後,我需要將其移動以便使用它。在iOS中,您將數據庫從應用程序移動到文檔文件夾,因爲您無法通過簽名來寫入應用程序。這是Android的原因嗎?所以回到我的問題。以下代碼不會正確複製數據庫,它只會複製其中的一部分。爲什麼這樣?我在這裏做錯了嗎?在Android上移動數據庫

private final static String DB_NAME = "klb.sqlite"; 
dbPath = "/data/data/" + context.getPackageName() + "/databases/"; // setting this in constructor 

(上面的線是私有成員,適用於以下兩個代碼的例子)

BufferedInputStream bis = new BufferedInputStream(context.getAssets().open(DB_NAME)); 
FileOutputStream fos = new FileOutputStream(dbPath + DB_NAME); 

while (bis.read() != -1) { 
    fos.write(bis.read()); 
} 

bis.close(); 
fos.flush(); 
fos.close(); 

然而這個工作(對不起懶惰有關重新縮進)

 InputStream inputStream = context.getAssets().open("klb.sqlite"); 
     OutputStream outputStream = new FileOutputStream(file); 

     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = inputStream.read(buffer)) > 0) { 
      outputStream.write(buffer, 0, length); 
     } 

     inputStream.close(); 
     outputStream.close(); 
+0

好吧,從你的數據庫文件放置位置以及放置位置? – user370305

+0

如果您看過,我寫在我的文章中 – LuckyLuke

回答

5
while (bis.read() != -1) { 
    fos.write(bis.read()); 
} 

仔細看看。您撥打read兩次,但write一次。您實際上正在跳過其他每個字節。

+0

啊!我忘了將它存儲在一個變量中。謝謝你。 – LuckyLuke

相關問題