How can I see the contents of a sqlite db of my app in a real device?

后端 未结 5 607
迷失自我
迷失自我 2021-01-06 04:39

I want to see the contents of my database created in my app in the device I deploied it. I can use sqlite commands in shell and see in emulator but not in a real device.

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-06 05:09

    Try this,

    You can copy your database in to your sdcard where you can see your database

       public void backup() {
            try {
                File sdcard = Environment.getExternalStorageDirectory();
                File outputFile = new File(sdcard,
                        "YourDatabase.db");
    
                if (!outputFile.exists()) 
                     outputFile.createNewFile(); 
    
                File data = Environment.getDataDirectory();
                File inputFile = new File(data,
                        "data/"+getPackageName()+"/databases/"+"YourDatabase.db");
                InputStream input = new FileInputStream(inputFile);
                OutputStream output = new FileOutputStream(outputFile);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = input.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }
                output.flush();
                output.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new Error("Copying Failed");
            }
        }
    

    Hope it helps.

提交回复
热议问题