2014-06-17 59 views
0

我已經在系統上連接了我的設備。但我無法在DDMS的文件瀏覽中查看我的數據庫。有沒有簡單的方法,所以我可以從android設備獲取數據庫文件?如何查看android手機中的sqlite數據庫

+1

在真實的設備是不可能看到你的數據庫。但是您可以將數據庫導出到SDCard中,然後在SQLite瀏覽器中打開。 –

回答

1

你必須遵循下面的步驟來實現你的任務: -

  1. 連接你的設備,並啓動在調試模式下的應用。

  2. 複製從您的應用程序文件夾中的數據庫文件到SD卡: 執行:

    ./adb -d殼「運行爲com.yourpackge.name貓/data/data/com.yourpackge。名稱/數據庫/ filename.sqlite> /sdcard/filename.sqlite」

  3. 拉數據庫文件到您的計算機:執行:

    ./adb拉/ SD卡/執行:./adb

  4. Inst所有的Firefox SQLLite管理器:https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

  5. 打開Firefox SQLLite Manager並從上面的步驟3打開您的數據庫文件。

+0

如何複製數據庫,因爲數據是空的?看[這裏](http://stackoverflow.com/questions/33560781/how-to-view-sqlite-database-using-real-device) – Hoo

1
private void importDB() { 
     try { 
      File sd = Environment.getExternalStorageDirectory(); 
      File data = Environment.getDataDirectory(); 
       if (sd.canWrite()) { 
       String currentDBPath = "//data//" + "<package name>" 
         + "//databases//" + "<database name>"; 
       String backupDBPath = "<backup db filename>"; // From SD directory. 
       File backupDB = new File(data, currentDBPath); 
       File currentDB = new File(sd, backupDBPath); 

      FileChannel src = new FileInputStream(currentDB).getChannel(); 
      FileChannel dst = new FileOutputStream(backupDB).getChannel(); 
      dst.transferFrom(src, 0, src.size()); 
      src.close(); 
      dst.close(); 
      Toast.makeText(getApplicationContext(), "Import Successful!", 
        Toast.LENGTH_SHORT).show(); 

     } 
    } catch (Exception e) { 

     Toast.makeText(getApplicationContext(), "Import Failed!", Toast.LENGTH_SHORT) 
       .show(); 

    } 
} 

private void exportDB() { 
    try { 
     File sd = Environment.getExternalStorageDirectory(); 
     File data = Environment.getDataDirectory(); 

     if (sd.canWrite()) { 
      String currentDBPath = "//data//" + "<package name>" 
        + "//databases//" + "<db name>"; 
      String backupDBPath = "<destination>"; 
      File currentDB = new File(data, currentDBPath); 
      File backupDB = new File(sd, backupDBPath); 

      FileChannel src = new FileInputStream(currentDB).getChannel(); 
      FileChannel dst = new FileOutputStream(backupDB).getChannel(); 
      dst.transferFrom(src, 0, src.size()); 
      src.close(); 
      dst.close(); 
      Toast.makeText(getApplicationContext(), "Backup Successful!", 
        Toast.LENGTH_SHORT).show(); 

     } 
    } catch (Exception e) { 

     Toast.makeText(getApplicationContext(), "Backup Failed!", Toast.LENGTH_SHORT) 
       .show(); 

    } 
} 

這裏是reference

相關問題