2014-06-17 49 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

相关问题