如何下载安卓应用程序的SQLite数据库?

3

我在我的Android应用程序中使用SQLite数据库。我想将数据库下载到电脑上。请问有人可以告诉我该怎么做吗?

3个回答

6

如果您的应用程序在模拟器中,您可以使用DDMS并打开/data/data/your.package.name/databases

如果您的应用程序在移动设备中。这是我将数据库复制到sdcard根目录文件夹的步骤。

    /** The name of the database file */
    static final String DATABASE_NAME = "mydatabase.db";
    static final String DATABASE_NAME_FULL = "/data/data/com.my.application/databases/" + DATABASE_NAME;

    public static boolean backUpDataBase(Context context){
    boolean result = true;

    // Source path in the application database folder
    String appDbPath = DATABASE_NAME_FULL;

    // Destination Path to the sdcard app folder
    String sdFolder =  Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + DATABASE_NAME;


    InputStream myInput = null;
    OutputStream myOutput = null;
    try {
        //Open your local db as the input stream
        myInput = new FileInputStream(appDbPath);
        //Open the empty db as the output stream
        myOutput = new FileOutputStream(sdFolder);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }
    } catch (IOException e) {
        result = false;
        e.printStackTrace();
    } finally {
        try {
            //Close the streams
            if(myOutput!=null){
                myOutput.flush();
                myOutput.close();
            }
            if(myInput!=null){
                myInput.close();
            }
        } catch (IOException e) { } 
    }

    return result;
}

您需要在manifest.xml文件中添加以下内容:

你需要这个

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

1

如果您的模拟器正在运行,您可以通过打开DDMS透视图(Window > Open perspective > DDMS)并打开data/data/your.package.name/databases来访问它,并将其拉到您的计算机上。

如果您的设备没有被root,您无法从设备中获取它。如果已经root,您也无法访问其所在的目录,因为它受到保护。您需要使用root浏览器将其复制到SD卡上,然后才能使用DDMS将其拉到计算机上。


不是 root 过的,现在该怎么办?:( 顺便说一句,这个数据库是我自己应用的数据库... - ahsan
那么我想你就没什么希望了。应用程序数据是私有的,这是有充分理由的。我认为你没有机会... - Knickedi

0

我通过使用Android调试桥来解决这个问题。要获取数据库,您必须在SDK工具目录中执行以下操作:

adb.exe exec-out run-as com.example.project cat databases/databasename > file.sqlite

其中com.example.project是来自清单文件的包名,databasename是您的数据库名称。该数据库将被放置在当前目录中。

它适用于模拟器和设备(已检查至API Level 28)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接