how to download the sqlite database of an android app?

倾然丶 夕夏残阳落幕 提交于 2020-01-12 10:23:39

问题


I am using a sqlite database in my android app. I want to download the database to pc. Can anyone kindly tell me what to do ?


回答1:


if your app is in emulator you can just use the DDMS and open /data/data/your.package.name/databases.

if you have your app in mobile. here is what i do to copy the db to the sdcard root folder.

    /** 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;
}

you need this in your manifest.xml

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



回答2:


If your emulator is running you can access it by opening the DDMS perspective (Window > Open perspective > DDMS) and open data/data/your.package.name/databases and pulling it onto your computer.

You can't get it from your device if it is not rooted. If it is you can't access the directory where it is located because it is protected. You have to copy it with a root explorer onto your SD card and then you can pull it with DDMS onto your computer.




回答3:


I solved this by using the android debug bridge. To get the database you have to execute in the directory of your sdk tools:

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

where com.example.project is the packagename from the manifes and databasename is the name of your database. The database will be put to your current directory.

It works on emulators and devices (checked up to API Level 28).



来源:https://stackoverflow.com/questions/7577196/how-to-download-the-sqlite-database-of-an-android-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!