Android SQLite Dump Database From Code For Error R

2019-01-28 11:31发布

问题:

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 (reference: http://www.sqlite.org/sqlite.html) into a string, which I can then email.

Does anyone know if there an easy way to do this from within an android program, or am I going to have interact with the android OS and run the commands that way?

回答1:

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.