Android SQLite Dump Database From Code For Error Reporting

怎甘沉沦 提交于 2019-12-18 09:01:42

问题


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.



来源:https://stackoverflow.com/questions/19459704/android-sqlite-dump-database-from-code-for-error-reporting

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