我正在进行一些调试,我想知道是否可以通过SDK显示手机SQLite数据库的内容?我知道可以通过手机查询来做到这一点。但我只是很好奇你能通过SDK做到吗?
答案 0 :(得分:4)
您可以使用Eclipse的文件管理器从设备获取文件,当它处于USB模式时从SD卡获取。您只有这个选项,因为您无法将设备放入Eclipse并同时安装SD卡。你必须使用Eclipse。
以下是将数据库导出到SDCard的代码
/*
* Task to backup the database to the SDCard
*/
public static class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
private Context ctx;
/**
*
*/
public ExportDatabaseFileTask(Context ctx) {
super();
this.ctx=ctx;
}
// automatically done on worker thread (separate from UI thread)
protected Boolean doInBackground(final String... args) {
File dbFile =
new File(Environment.getDataDirectory() + "/data/[com.your.pkg]/databases/[pkg]");
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, dbFile.getName());
try {
file.createNewFile();
this.copyFile(dbFile, file);
return true;
} catch (IOException e) {
Log.e("birthdroid", e.getMessage(), e);
return false;
}
}
// can use UI thread here
protected void onPostExecute(final Boolean success) {
if (success) {
Toast.makeText(ctx, "Export successful!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ctx, "Export failed", Toast.LENGTH_SHORT).show();
}
}
void copyFile(File src, File dst) throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
}
在光标上你总是可以打电话:
DatabaseUtils.dumpCursorToString(CUR);
获取游标的原始字符串表示