gpt4 book ai didi

android - 如何通过SDK显示手机的SQLite数据库?

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:42 26 4
gpt4 key购买 nike

我目前正在调试,我想知道是否可以通过 SDK 显示手机 SQLite 数据库的内容?我知道可以通过电话查询来做到这一点。但我很好奇你能通过 SDK 做到这一点吗?

最佳答案

  • 将数据库导出到 sdcard 文件,每次你必须复制到你的计算机,并通过一些 SQLite 管理器工具打开,我使用 Firefox 的插件。很简单,我不必一次又一次地重新打开数据库,只需点击刷新按钮,表格就会更新。

当设备处于 USB 模式时,您可以使用 Eclipse 的文件管理器从设备、从 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);

获取光标的原始字符串表示

关于android - 如何通过SDK显示手机的SQLite数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3093012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com