Android SQLite转储数据库中的错误报告代码 [英] Android SQLite Dump Database From Code For Error Reporting
问题描述
我正在为我的一个Android程序开发一种诊断工具。
I'm working on a diagnostic tool for one of my android programs.
本质上,如果用户遇到问题,我想做点什么就像一个SQLite 转储
(参考: http:// www .sqlite.org / sqlite.html )转换为字符串,然后我可以通过电子邮件发送该字符串。
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.
有人知道是否有一种简单的方法可以从android程序中执行此操作,还是我要与android操作系统进行交互并以这种方式运行命令?
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?
推荐答案
我解决了自己的问题,方法是将数据库转储到文件中,然后将文件读取为字符串。对于那些感兴趣的人,下面为您提供了总体思路:
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
因此上述内容存在问题,但我没有看到它,因为我从adb运行了 SQLite3
命令。
So the above had an issue and I didn't see it, because I ran the SQLite3
command from the adb.
看起来像 SQLite3
是可以从 adb
运行的命令,但不一定在电话。基于此,我采取了另一种方法。我使用android的DatabaseUtils来帮助我建立所需的结果,例如 DatabaseUtils.dumpCursorToString(cursor)
。我对我感兴趣的每个表都执行了此操作,并构建了一个返回字符串。
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.
这篇关于Android SQLite转储数据库中的错误报告代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!