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

标签 android database

我目前正在调试,我想知道是否可以通过 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/

相关文章:

java - 我可以使用NotificationCompat.BigTextStyle而不会在旧设备上崩溃吗?

java - retrofit 2.3.0 如何处理嵌套的json?

javascript - 类型错误 : Cannot read property 'user_first_name' of undefined

php - mysqli - 对非对象 mysqli 上的成员函数 fetch_array() 的 fetch_Array 错误调用

sql-server - 哪个更好 :Filter on join clause or Filter on where clause when joining two table?

java - 在Android中使用外部数据库文件

android - Android 8.0 缺少通知样式

android - 从 LiveData 触发第二个查询并合并结果(Firestore)?

mysql - 按字符串或整数搜索/索引

android - JSON 中的最后一张图片未显示