Android SQLite Dump Database From Code For Error Reporting

前端 未结 1 619
故里飘歌
故里飘歌 2020-12-21 06:25

I\'m working on a diagnostic tool for one of my android programs.

In essence, if a user is having issues, I\'d like to do something like an SQLite dump

1条回答
  •  有刺的猬
    2020-12-21 07:02

    I solved my own problem, by doing a dump of the database to a file, then reading the file into a string. For those interested, the following gives you the general idea:

        String dbPath = "/data/data/com.company.project/databases/project.db";
        String outputPath = "/data/data/com.company.project/databaseDump.txt";
    
        Process proc = Runtime.getRuntime().exec("sqlite3 " + dbPath + " '.dump' > " + outputPath);
        proc.waitFor();
        returnString = FileUtil.getStringFromFile(outputPath);
    

    EDT

    So the above had an issue and I didn't see it, because I ran the SQLite3 command from the adb.

    It looks like SQLite3 is a command you can run from the adb, but it is not necessarily on the phone. Based on that I took a different approach. I used android's DatabaseUtils to help me build the result I needed, e.g. DatabaseUtils.dumpCursorToString(cursor). I did this for each of the tables I was interested in and built a return string.

    0 讨论(0)
提交回复
热议问题